Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is intended to determine whether a user-input string is a palindrom

ID: 3623685 • Letter: T

Question

This program is intended to determine whether a user-input string is a palindrome or not. Only iterative methods should be used; no recursion allowed. The program should ignore special characters and spaces. My program seems to do fine with special characters, but gets it wrong when spaces are included. I have no idea why. I'm sure it's simple, but I've done everything I can think of. Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* function prototype */
void Palindrome( char* );

/* beginning of function main */
int main ()
{
/* declaration of variables */
char string[ 100 ];


/* request and store user input */
printf( "Please enter a string: " );
scanf( "%s", string );



Palindrome( string );

return 0;

} /* end function main */

/* begin function Palindrome */
void Palindrome( char* string )
{
int length = strlen( string );
int i = 0; /* index variable to first character of string */
int j = length - 1; /* index variable to last character of string */

int k; /* counter */

for( k = 0; k <= length; k++) {

/* increment or decrement past those characters that are neither letter nor numbers */
while( isalnum( string[ i ] ) == 0 || isblank( string[ i ] ) == 1 ) {
i++;
}

while( isalnum( string[ j ] ) == 0 || isblank( string[ j ] ) == 1 ) {
j--;
}

/* if front and rear indices meet, string is a palindrome */
if( i > j ) {
printf( "The string is a palindrome. " );
{break;}
}
/* if two characters don't match, string is not a palindrome */
else if( (tolower( string[ i ])) != (tolower( string[ j ])) ) {
printf( "The string is not a palindrome. " );
{break;}
}
/* move to and check the next two characters */
else {
i++;
j--;
}


} /* end for */
} /* end function Palindrome */

Explanation / Answer

My bad, I didn't actually do any testing until now. Your problem is the SCANF %s, it only picks up the characters up to a space - so all you get is the first part of your palindrome phrase - and it obviously never will be considered one since it's incomplete!
Here is a rewrite, this time we use fgets() and here is where the isspace() is useful because it detects new lines as well, these get picked up with the fgets() and it can be pretty annoying to handle. Hope this helps better. I tested it with "abb a, ab ba, abba, and robert trebor", works fine now. :)
===================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* function prototype */
void Palindrome( char* );

/* beginning of function main */
int main ()
{
    /* declaration of variables */
    char string[100];


    /* request and store user input */
    printf( "Please enter a string: " );
   
    fgets(string, sizeof string, stdin);
   
    Palindrome( string );
   
    return 0;

} /* end function main */

/* begin function Palindrome */
void Palindrome( char* string )
{
    int length = strlen( string );
    printf("this is our string %s ",string);
    int i = 0; /* index variable to first character of string */
    int j = length - 1; /* index variable to last character of string */
   
    int k; /* counter */

    for( k = 0; k <= length; k++) {

        /* increment or decrement past those characters that are neither letter nor numbers */
        while( isalnum( string[ i ] ) == 0 || isspace( string[ i ] ) != 0 ) {
        i++;
        }

        while( isalnum( string[ j ] ) == 0 || isspace( string[ j ] ) != 0 ) {
        j--;
        }

        /* if front and rear indices meet, string is a palindrome */
        if( i > j ) {
            printf( "The string is a palindrome. " );
            {break;}
        }
        /* if two characters don't match, string is not a palindrome */
        else if( (tolower( string[ i ])) != (tolower( string[ j ])) ) {
            printf( "The string is not a palindrome. " );
            {break;}
        }
        /* move to and check the next two characters */
        else {
            i++;
            j--;
        }


    } /* end for */
} /* end function Palindrome */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote