Question:
C programming palindrome problem...?
Levi E
2009-11-07 16:46:58 UTC
I'm trying to write a program with four separate functions to tell me if the name entered is a palindrome. I believe my problem is with my function that takes the first and last name string and creates a new string with only the first name, and I also think I might have some problems in my function that reverses the first name string and creates a new reversed string of the first name. My program doesn't print out anything. I don't have a clue what to do and any help would be absolutely great! Thanks for any answers!

Here is my code:

#include
#include
#include
#define MAX 50

void get_str( char str[], int size )
{
int k=0;
char c;

while( 1 )
{
c = getchar();

if( c == '\n' )
break;

if( k > size-2 )
{
printf( "Error: array too small\n" );
break;
}

str[k] = c;
k++;
}
str[k] = '\0';
}
char copy_first_name( char str[], char firstn[] ) /*string that creates first name string from the first and last name string*/
{
int i, ch;
i = 0;
ch = str[i];
while (isalpha(ch) == 1)
{
firstn[i]=str[i];
i++;
}
firstn[i] = '\0';
return 0;
}
char reverse_str( char str[], char revstr[] ) /*function that creates a new reversed string*/
{
int i, j;
i = 0;
j = 0;
for( i = strlen(str)-1; i > 0; i--)
{
revstr[j] = str [i];
j++;
}
revstr[j]= '\0';
return 0;
}

char is_same( char a[], char b[] )
{
int r;
r = strcmp( a, b );
if (r = 0)
return 0;
else
return 1;
}
int main()
{
char full_name[MAX], first_name[MAX], reversed[MAX];
int same;

printf( "Please enter your first and last name: " );
get_str( full_name, MAX );

copy_first_name( full_name, first_name );
printf( "Your first name is: %s\n", first_name );

reverse_str( first_name, reversed );
printf( "Your first name reversed is: %s\n", reversed );

same = is_same( first_name, reversed );
if( same == 1 )
printf( "Your first name is a palindrome!\n" );
else
printf( "Your first name is not a palindrome.\n" );
return 0;
}
Three answers:
alf.R
2009-11-08 09:34:45 UTC
Take a look my funct:

#include

#include

int strlen(const char* word){

int len=0;

while(*word++)len++;

return len;

}



char* strcpy(char* dest, const char* source){

char *temp=dest;

while(*dest++=*source++);

return temp;

}



char* stricpy(char* dest, const char* source){

char *temp=dest;

while(*source){

if(isalpha(*source)) *dest++=tolower(*source);

*source++;

};

*dest=0;

return temp;

}



int isplndrom(char * word){

if(*word){

int len = strlen(word);

char a=*word;

char b=*(word+len-1);

int it_is=a^b;

if(it_is) return 0;

*(word+len-1)=0;

*word++;

return isplndrom(word);

}

else return 1;

}



/*

return 1 if palindrome

return 0 if not palindrome

*/

int palindrom(const char* word){

int len=strlen(word);

char *icasestr=new char[len+1];

stricpy(icasestr,word);

len=isplndrom(icasestr);

delete icasestr;

return len;}



void main(){

cout<
cout<
cout<
cout<


}
Just Jess
2009-11-07 17:58:58 UTC
Took me a second too.



Here's the big problem:



inside copy_first_name,



ch = str[i];

while (isalpha(ch) == 1)

{



If you look at that for just a sec, it'll all click.



But basically, ch is defined one time, outside of the loop, so you're basically doing this



i=0;

firstn[0] = '\0';



which is when the program dutifully gives you the empty string you asked it for.



Also, isalpha(ch) doesn't necessarily return 1 on success, it returns > 0.



That'll get you started. There's a couple more, but you had no way of debugging them without the output from that function.



EDIT:

Just a style note,



Since he's using C, not C++, any advice involving "cout" etc is wrong. And it's usually good form to leave things in the same form the other person is using them in. For instance, I usually define strings like



char *somestring = NULL;



(allocating and freeing memory as necessary from that point)



instead of his way,

char somestring[LENGTH]



But it would be wrong of me to post a code example in pointer form instead of array form.



Advice is a lot more useful when the other person can understand it immediately.
Velammal College Of Engineering And Technology.
2014-11-23 04:48:02 UTC
https://answersrip.com/question/index?qid=20101003160224AA9UBH9


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...