-
string editing
i am creating a program to see if a word is a palindrome, but i want it, when the word is read, to ignore the spaces and punctuation in the word/sentance. i am using apstring. how would i og about doing this.
for example, if i type "madam i'm adam", the program reads the spaces, and backwards it reads "mada m'i madam" which is wrong, i want it to ocmpletly ignore spaces. puct. thanks
tony
-
find an ascii table, when going through your string to reverse it, add only valid ones(a-z & A-Z) to your string....to check you can use it as an integer
int test;
apstring myteststr;
test = myteststr[place]
if(test == ....
:)
-
find an ascii table, when going through your string to reverse it, add only valid ones(a-z & A-Z) to your string....to check you can use it as an integer
int test;
apstring myteststr;
test = myteststr[place]
if(test == ....
:)
-
find an ascii table, when going through your string to reverse it, add only valid ones(a-z & A-Z) to your string....to check you can use it as an integer
int test;
apstring myteststr;
test = myteststr[place]
if(test == ....
:)
-
Just use a regular C function -
Code:
#include <ctype.h>
BOOL IsPalindrome(char *instr){ // return 1 = YES 0=NO
char tmp[100],tmp1[100];
char *buf,*s;
int i;
buf=tmp;
s=instr;
// remove punctuation & uppcase the result
while (*s!=0x00) {
if(!ispunct(*s)) *buf++ = toupper(*s);
s++;
}
buf=0x00; // null terminate the string
_strrev(tmp1,tmp); // reverse the string
return (strcmp((tmp1,tmp)==0); // compare
}
-
check the MSDN help in visual studio
type _strrev and look at the example....
the sample function given shows you...oddly an enough...
an example of how to test if a string is a palindrome
-
I recommend using isalpha and tolower to make a clean copy of the string, then reverse it using strrev and test with strcmp.
Code:
for(char* p = str, char* q = buf; *p != '\0'; p++, q++)
if(isalpha(*p)) *q = tolower(*p);
*q = '\0';
// length of copy = q - buf