d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Iso Programing Help With Hw! > Programming In C
Add Reply New Topic New Poll
Member
Posts: 9,330
Joined: Oct 2 2006
Gold: 0.00
Oct 23 2013 01:30pm
Purpose: Use arrays & strings.

Description:

Write a function that determines if a string contains a
single word palindrome.

Pass a string to the function and have it return true or
false indicating whether the string contains a single
word palindrome.


Okay so here is where i am at.
I have a function to check for palindrome but now I need to have one function that tests a whole phrase instead of just a word which means i need it to ignore spaces and caps.


Below is the code I have that tests for palindrome although it doesnt ignore spaces or caps which is where i guess i need help.
void pal(char *str1)
{
int i;
int x;
x = strlen(str1)-1;
for ( i = 0; i<=x; i++)
{
if(str1[i] == str1[x-i])
{
printf(" Your String is a Palindrome \n");
return 0;
}
else
{
printf(" Your String is not a Palindrome \n");
return 0;
}
}
}



In the end goal is to have one functions that tests a phare for palindrome and ignores spaces and caps.
Member
Posts: 16,796
Joined: Oct 15 2008
Gold: 19,640.61
Oct 24 2013 09:51pm
[I]void pal(char *str1)
{
int i;
int x;
x = strlen(str1)-1;
for ( i = 0; i<=x; i++)
{
if(str1[i] == str1[x-i])
{
printf(" Your String is a Palindrome \n");
return 0;
}
else
{
printf(" Your String is not a Palindrome \n");
return 0;
}
}
}

Methinks that you are comparing the 1st and last letter and calling it a day by returning 0. I would increment i in the if statement: if(str1[i] == str1[x-i]). And you only need to increment i to range x/2.


Member
Posts: 3,715
Joined: Mar 15 2011
Gold: 4,703.80
Oct 25 2013 04:14am
I'd do a few things differently.

void pal(char *str1)
{

int i;
int x;
x = strlen(str1)-1;

for (i = 0; i<=x; i++)
{
if (str1[i] != str1[x-i])
{
printf(" Your String is a not a Palindrome \n");
return 0;
}
}

printf(" Your String is a Palindrome \n");
return 1;

}



/edit: Keep going.. you're on the right track.



This post was edited by Dmon_Hunter on Oct 25 2013 04:15am
Go Back To Homework Help Topic List
Add Reply New Topic New Poll