Thread: Exercise on Strings

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Unhappy Exercise on Strings

    Hello everyone,
    i have to program an C++ program that will determine whether a given string is a palindrome or not.(A palindrome is a word that read same forward and backward...like "MADAM")

    Here is my program, but i got some error and i don't know why?
    Please help.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You have a lot of errors in that program syntactic and logical. Here's the fixed program :-)
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    	int length;
    	int arr1=0;
    	typedef char string[30];
    	typedef char string2[30];
    	string word[10];
    	string2 invWord[10];
    	cout<<"Enter word"<<endl;
    	cin>>word[arr1];
    	
    	
    	while (stricmp(word[arr1], "Quit")!= 0)
    	{
    		length=strlen(word[arr1]);
    		
    		for (int j=0, i=length-1; j<length; j++, i--)
    		{
       			invWord[arr1][j]=word[arr1][i];
       		}
    
    		invWord[arr1][length] = '\0';
    
    		if (strcmp(word[arr1], invWord[arr1]) == 0)
       			cout<<"Palindrome"<<endl;
    
       		cout<<"Enter word"<<endl;
       		cin>>word[++arr1];
    	}
    
       	return 0;
    }
    But you can do a lot better, here's my stab at it :-)
    Code:
    #include <functional>
    #include <algorithm>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class good_char: public unary_function<char, bool> {
      const string good;
    public:
      good_char()
        : good("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
      {}
    
      bool operator()(const char c) const
      {
        if (good.find(c) != string::npos)
        {
          return true;
        }
    
        return false;
      }
    };
    
    bool is_palindrome(string& s)
    {
      string::iterator new_end = remove_if(s.begin(), s.end(), not1(good_char()));
      s.erase(new_end, s.end());
    
      string r = s;
    
      reverse(r.begin(), r.end());
    
      if (s == r)
      {
        return true;
      }
    
      return false;
    }
    
    int main()
    {
      string s;
    
      cout<<"Enter a word or phrase: ";
    
      getline(cin, s);
    
      if (is_palindrome(s))
      {
        cout<<"You entered a palindrome"<<endl;
      }
    }
    *Cela*

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    hey.

    hey cela,
    just wanted to make things clear in my head . What does these do?
    Code:
    class good_char: public unary_function<char, bool> {
      const string good;
    public:
      good_char()
        : good(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
    YZ")
      {}
    
      bool operator()(const char c) const
      {
        if (good.find(c) != string::npos)
        {
          return true;
        }
    
        return false;
      }
    };
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>What does these do?
    It's a function object that checks a character given to it with a list of valid characters. If it doesn't match one of those characters then it returns false and remove_if() deletes the character, just what we want. :-)
    *Cela*

  5. #5
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    and hey cela ,
    sorry again me here. what does this do??
    Code:
    string::npos
    and lastly why is this empty function?
    good_char()
        : good("  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
    
    YZ")
      {//why is it empty here?}
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Re: string

    hi cela,

    unfortunately, i didn't start the true 2 dimensional array yet("array [] []").

    So, can you change the program without using the "real" 2 dimensional array?

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
      int length;
      typedef char string[30];
      typedef char string2[30];
      string word;
      string2 invWord;
      cout<<"Enter word"<<endl;
      cin>>word;
    
      while (stricmp(word, "Quit")!= 0)
      {
        length=strlen(word);
    
        for (int j=0, i=length-1; j<length; j++, i--)
        {
          invWord[j]=word[i];
        }
    
        invWord[length] = '\0';
    
        if (strcmp(word, invWord) == 0)
           cout<<"Palindrome"<<endl;
    
        cout<<"Enter word"<<endl;
        cin>>word;
      }
    
      return 0;
    }
    *Cela*

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Smile Re: String

    Thanks Cela,

    but i don't understand this line:

    invWord[length] = '\0'

    Without this line, the program doesn't work, why?
    if "invWord" need this line, how come "word" don't need this line?

    Thanks

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > invWord[length] = '\0'

    This puts a null terminator at the end of the string - it tells the computer the string's done. word doesn't need one because it has one appended to it when it's read in in the cin statement.

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just wanted to toss in my 2 cents. Obviously it's going to have a lot less overhead as I've excluded any use of the stl, and it's only 4 lines long

    Code:
    bool isPalindrome(const char *str) {
      for (int l = 0, r = strlen(str) - 1; l < r; ++l, --r)
        if (str[l] != str[r])
          return false;
      return true;
    }

  11. #11
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Obviously it's going to have a lot less overhead as I've excluded any use of the stl, and it's only 4 lines long
    It also doesn't have much flexibility. Since you're making an exact comparison the following palindrome returns false
    Code:
    a man, a plan, a canal, panama
    *Cela*

  12. #12
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Oh, yeah. You're right. I didn't take that into consideration. How about this then:
    Code:
    bool isPalindrome(const char *str) {
      for (int l = 0, r = strlen(str) - 1; l < r; ++l, --r) {
        while (!isalpha(str[l]))
          ++l;
        while (!isalpha(str[r]))
          --r;
        if (l < r && toupper(str[l]) != toupper(str[r]))
          return false;
      }
      return true;
    }
    Last edited by LuckY; 01-29-2003 at 10:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM