Results 1 to 7 of 7

Thread: string editing

  1. #1
    t420h
    Guest

    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

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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 == ....



  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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 == ....



  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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 == ....



  5. #5
    jim mcnamara
    Guest
    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
    
    }

  6. #6
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width