Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (lgbasallote):

Can someone "proofread" this C program code? What the program does: The user inputs a word, and then the program will tell the user if the inputted word is a palindrome (a word that is read the same backwards like LEVEL, MADAM, RADAR, etc) or not.

OpenStudy (lgbasallote):

``` #include <stdio.h> #include <conio.h> #include <stdlib.h> main() { int letters, cntr, var; char word[100]; printf("\n How many letters? "); scanf("%d", &letters); printf("\n\n Enter word > "); fflush (stdin); gets(word); for (cntr = 1; cntr < letters; cntr++) { if (word[cntr - 1] == word[letters - cntr]) var = 1; else var = 0; } if (var == 1) printf("\n '%s' IS A PALINDROME", word); else printf("\n '%s' IS NOT A PALINDROME", word); printf("\n\n\n "); system("pause"); } ```

OpenStudy (lgbasallote):

if something's wrong, please tell me which it is, and how to fix it

OpenStudy (anonymous):

what is your question?

OpenStudy (anonymous):

this is wrong code for PALINDROME

OpenStudy (anonymous):

give sting as "eggt" will tell as PALINDROME..which is not true.

OpenStudy (anonymous):

you should break the loop if 1 does not match.

OpenStudy (anonymous):

break the for loop if even one miss match occur. and why are you asking for the letter? because you are using static char array not dynamic.

OpenStudy (lgbasallote):

i did not get a single thing from that....

OpenStudy (anonymous):

#include <stdio.h> #include <conio.h> #include <stdlib.h> main() { int letters, cntr, var; char word[100]; printf("\n How many letters? "); scanf("%d", &letters); printf("\n\n Enter word > "); fflush (stdin); gets(word); var=0; for (cntr = 1; cntr <= letters; cntr++) { if (word[cntr - 1] == word[letters - cntr]) var = 1; else { var = 0; break; } } if (var == 1) printf("\n '%s' IS A PALINDROME", word); else printf("\n '%s' IS NOT A PALINDROME", word); printf("\n\n\n "); system("pause"); } that is rightly working. two mistakes: 1)at for loop 2)at the var==0 condition

OpenStudy (anonymous):

and if yu want a trace of program then here is what it does it stores your string into array,reads character from first and last ,compares them if equal,then it goes for next character i.e second and second last .

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!