Palidromes

This is a discussion on Palidromes within the C++ Programming forums, part of the General Programming Boards category; how do you write a program about a palidrome withoust using the STL. That is create a program that takes ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Palidromes

    how do you write a program about a palidrome withoust using the STL.

    That is create a program that takes a string of character like "civic or rotator" and check if it is a palidrome without using the standard template library.

    thanks

  2. #2
    stovellp
    Guest
    I would start by doing something like:

    Getting the size of the string.
    Looping through with two counters, one set to 0, the other at the size of the string -1 (?), and comparing each letter of the string to see if they match, until i get to the middle letter (if there is one)

    Code:
    #include <string>
    
    char String[50];
    int C1 = 0;
    int C2 = 0;
    
    int main()
         {
         // Get the string
         C2 = strlen(String);
    
    
         
         while (C1 != (strlen(String) / 2))
              {
              if (String[C1] != String[C2])
                   cout << "Not a pallendrome!";
              else
                   C1++; C2--;
              }
         }
    Theres other ways you might compare though, such as comparing the letters in the loop and setting flags or something, but thats up to you.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Code:
    #include < iostream >
    #include < cstring >
    
    bool isPalindrome( char *start )
    {
        // point at the last element
        char *end = start + std::strlen ( start ) - 1;
    
        // case insensitive comparison
        std::strlwr ( start );
    
        do{
            if ( *start != *end ) 
                return false;
        }while ( ++start < --end );
    
        return true;
    }
    
    int main()
    {
        char candidate[] = "Civic or rotator";
    
        std::cout << candidate;
    
        if ( isPalindrome( candidate ) ) 
            std::cout << " is a palindrome.\n";
        else
            std::cout << " is not a candidate.\n";
    
        return 0;
    }
    edit: typo.
    Last edited by Codulation; 01-30-2003 at 02:48 AM.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by stovellp
    Code:
    #include <string>
    
    char String[50];
    int C1 = 0;
    int C2 = 0;
    
    int main()
         {
         // Get the string
         C2 = strlen(String);
    
    
         
         while (C1 != (strlen(String) / 2))
              {
              if (String[C1] != String[C2])
                   cout << "Not a pallendrome!";
              else
                   C1++; C2--;
              }
         }
    This will trap you in an infinite loop printing "Not a pallendrome!" because String[C2] the first time through will point at the terminating null, causing control to enter the body of the if statement. Since else will be skipped, this will continue, and continue....

    edit: There's more problems here. the C2 decrement is not part of the else statement (no brackets) and <string> is included, but no std namespace is used. BTW string is the STL string container, cstring or string.h is the C-style string header.

    -Futura
    Last edited by Codulation; 01-30-2003 at 03:02 AM.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  5. #5
    stovellp
    Guest
    Yeah your right, if I had compiled and tested this I would have fixed those errors. Its amazing what silly mistakes i make when I dont actually test my code .

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    It is also amazing that people will do other peoples homework for them, and that questioners asking one of the commonest basic homework assignments out do not search the board for the dozens of other times this has been asked here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    stovellp
    Guest
    Yeah but hey, I asked thousands of basic questions back in my younger days here (about a year ago), although they weren't for homework. So I thought I ought to give something back, since I did (almost, close enough) know the answer to this.

    All I would have done differently was to have the counters Increment and decrement in the loop not in the IF statement and start C2 at one less than it was. I didn't expect my code would be used, and so didn't put much into doing it, it was meant to serve as an example of what could be done.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    > It is also amazing that people will do other peoples homework for them


    You're absolutely right adrianxw, my bad. No more free answers from me! Although I will say that for me personally, I have learned as much by scrutinizing well-written code as by figuring something out by myself. There's a place for both. I suppose it also has to do with your desire and integrity as a learner.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I suppose it also has to do with your desire and integrity as a learner.

    That is so true.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    WRong answer

    Hey for you information this is not an homework. This is just something i tried doing on my own.
    See the palidrome you guys are doing i know that, but what i was asking was

    could you test for a palidrome wiothout using the STL's we have like
    #include<string>

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    and that questioners asking one of the commonest basic homework assignments out do not search the board for the dozens of other times this has been asked here.
    <<<

    Closed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21