English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i'm trying to create a program that will tell whether a string of characters is a palindrome. how could i assign the characters so that the first letter will equal the last letter and the 2nd will equal the 2nd to last and so on? if anyone knows how and could provide an example, that would be awesome.

2007-09-19 14:03:59 · 4 answers · asked by Ali 3 in Computers & Internet Programming & Design

4 answers

Hi,
Try the following
----------------------------------------------
#include
#include
#include

void main()
{
char *s,*r;
s="ABCD";
strcpy(s,r); // make a copy of the string
strrev(s); // reverse the stirng
if(strcmp(s,r)==0) //compare both the strings
printf("String is a palindrome.");
else
printf("String is not a palindrome.");
getch();
}
----------------------------------------------------------------

2007-09-19 17:17:29 · answer #1 · answered by iqbal 4 · 0 0

hi. about your question here's the logic
1.) get the input from user (String "input" for example)
2.) eliminated the spaces in the string otherwise even if its a palindrome, the system will compare a letter to a space
3.) to compare the first letter to the last letter you cud do this:
a.) declare another string then put the reversed "input" in that string (using for loop), then use "strcmp" get that?
b.) or use a for loop

for(int x=0; x {
if !(input[x]=second[input.length()-x])
break;
}


the logic is pretty much like that but i'm not sure if the syntax is correct since i programmed it directly to the "Your Answer" field... so bear with me.. :D

thanks!!! email me if you have questions!!!

2007-09-19 14:44:08 · answer #2 · answered by Anonymous · 0 0

something like this? (note: i am more of a java programmer, but i have gone into C++ a bit)

char stringchars[9999]; //just in case someone types a string
//19998 characters long...
for(int i=0;i stringchars[i]=yourstring[i];
}
bool isPalindrome=true;
int j=0;
for(int i=yourstring.length()-1;i>ceil(yourstring.length()/2);i--){
if(!stringchars[j++] == yourstring[i]){
isPalindrome=false;
break;
}
}
if(isPalindrome){
cout<

Didn't check it, but i *think* it works...(i haven't coded in c++ for a few months now)

2007-09-19 15:16:16 · answer #3 · answered by Anonymous 3 · 0 0

Here is a link to a code snippet for palidromes.

2007-09-19 15:57:17 · answer #4 · answered by alints_2000 4 · 0 0

fedest.com, questions and answers