linn
2011-03-04 20:03:14 UTC
if (array1[i] == array2[i])
It would compare if the two were equal. If they were equal, then I'd be able to say that the user input was a palindrome. However, it completely ignores the else statement (that it's NOT a palindrome) and prints that everything is a palindrome. What am I doing wrong?
Note: The line "if (strcmp(input, array) == 0)" is just there so that the program will work, but I'm trying to compile it WITHOUT using the string.h library
Here's my code:
#include
#include
#define STRLEN 50
int
main(void)
{
char input[STRLEN];
char array[STRLEN];
int i;
int total;
printf("Please enter a string > ");
scanf("%s", input);
total = 0;
for (i = 0; input[i] != '\0'; ++i) {
total = total + 1;
}
for (i = 0; i < total; i++) {
array[i] = input[total - 1 - i];
}
array[i] = '\0';
if (strcmp(input, array) == 0)
printf("Given string \"%s\" is a palindrome\n", input);
else
printf("Given string \"%s\" is not a palindrome\n", input);
return (0);
}