|
-
Jun 3rd, 2002, 04:16 PM
#1
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
-
Jun 3rd, 2002, 04:38 PM
#2
Frenzied Member
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 == ....
-
Jun 3rd, 2002, 04:39 PM
#3
Frenzied Member
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 == ....
-
Jun 3rd, 2002, 04:40 PM
#4
Frenzied Member
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 == ....
-
Jun 3rd, 2002, 09:17 PM
#5
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
}
-
Jun 3rd, 2002, 10:15 PM
#6
Hyperactive Member
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
Bababooey
Tatatoothy
Mamamonkey
-
Jun 4th, 2002, 06:08 AM
#7
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|