Levi E
2009-11-07 16:46:58 UTC
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;
}