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.
``` #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"); } ```
if something's wrong, please tell me which it is, and how to fix it
what is your question?
this is wrong code for PALINDROME
give sting as "eggt" will tell as PALINDROME..which is not true.
you should break the loop if 1 does not match.
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.
i did not get a single thing from that....
#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
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 .
Join our real-time social learning platform and learn together with your friends!