Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Testing a Palindrome (C++)

    i've got the program to produce an entered string in reverse, but i need a way to check whether or not it is a palindrome (ex. radar, racecar)

    //palindrome.cpp
    #include <iostream.h>
    #include <math.h>
    #include <string.h>

    int main()
    {
    char original[20],reversed[20];
    int length;
    cout <<"Enter a string"<<endl;
    cin.get (original,20);
    cin.ignore(80,'\n');
    length=strlen(original);
    strcpy(reversed,original);
    for (int i=0;i<=length-1;i++)
    {
    reversed[length-1-i]=original[i];
    }
    cout <<original<<" "<<reversed<<endl;
    return 0;
    }

    what if statement could i use?
    if ()
    cout <<"It is a palindrome"<<endl;

  • #2
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just go through the string and test it, something like this.

    Code:
    int i, j, ispalin;
    
    for(i = 0, j = strlen(string)-1, ispalin = 1; j > i; j--, i++)
    {
       if(toupper(string[i]) != toupper(string[j])) {ispalin = 0; break;}
    }
    
    printf("String %s %s a palindrome", string, (ispalin ? "is" : "is not"));
    Not sure if that's bug-proof, though.

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Have you two read our policy on posting Homework Assignments?

    Homework Assignments

    The reason I mention that is because writing a palindrome or string reversal program are extremely common beginning programming course assignments.

    DrEvil I hope they get a good grade for your work.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    umm this is not a homework assignment

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not sure if it's the best way to do it :S but worked with me

    Code:
    bool isPalindrome(string str1){
        int n;
        int str1size=str1.size();
        string str2;
        int misMatcheCount=0;
        
        for (int i=(str1size-1);i>=0;i--){
            n=(str1size-1)-i;
            str2[n]=str1[i];
            }
    
            
        for (int i=0;i<=(str1size-1);i++){
         if (str1[i]!= str2[i]){
         misMatcheCount++;
            }
         }
         
         if (misMatcheCount)
         return false;
         else return true;
         
         }

  • #6
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Palindrome

    Here's one I wrote, it's been a long time since you asked but maybe somebody else is wondering the same question. My approach is very simple, split the string into two strings, reverse the second half and compare. i test for an odd amount of letters in the string, if so I just increment the hslen by 1 in the line s2.substr(hslen+1, hslen);. That ignores the middle character if it's a palindrome. like in a word radar, it will ignore the d in the centre and test the characters around it.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void rString(string s, char *c0)
    {
    	int i = 0;
    	int j = s.size();
    	j--;
    
    	while (j != -1)
    	{
    		c0[i] = s[j];
    		j--;
    		i++;
    	}
    	c0[i] = '\0';
    }
    
    int main()
    {
    	string s = "";
    	string s1 = "";
    	string s2 = "";
    	cout << "Enter a string to test for a Palindrome: ";
    	getline(cin,s);
    	int i = 0;
    	int slen = s.size();
    	int hslen = slen/2;
    	char *c0;
    	c0 = new char[hslen+1]; // allocate new memory for char
    
    	if (slen%2 == 0) // string is even, split the string in half, reverse second half and compare two pieces
    	{
    		s1 = s.substr(0, hslen); // first half of string into s1
    		s2 = s.substr(hslen, hslen); // second half of string into s2
    		rString(s2, c0); // reverse s2 
    		const char *c1 = s1.c_str();
    		i = strcmp(c0, c1); // compare s1 with s2 (converted to char array's)
        	
    		if (i == 0)
    		{
    			cout << "Paaaaallllliiiiinnnnnddddddddrrrrrrrroooooooommmmmmmeeeeeeeee.........: " << s << endl;
    		}
    		else
    		{
    			cout << "sooooorrry not a palindrome" << endl;
    			delete[] c0;
    			return 0;
    		}
    	}
    	if (slen%2 != 0) // string is odd, ignore centre char and test the two even halves
    	{
    
    		s1 = s.substr(0, hslen); // move the first half of the string to s1
    		s2 = s.substr(hslen+1, hslen); // ignore the middle odd char and move the second half of the string to s2
    		rString(s2, c0); // reverse the second half of the string
    		const char *c1 = s1.c_str(); // convert s1 to char to test c0 vs c1 with strcmp();
    		i = strcmp(c0, c1);
        	
    		if (i == 0) // if i = 0 c0 and c1 are the same
    		{
    			cout << "Paaaaallllliiiiinnnnnddddddddrrrrrrrroooooooommmmmmmeeeeeeeee.........: " << s << endl;
    		}
    		else
    		{
    			cout << "sooooorrry not a palindrome" << endl;
    			delete[] c0;
    			return 0;
    		}
    	}
    	
    
    	delete[] c0;
    }

    I hope it helps.

  • #7
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    another way to do it is to check first and last alphabet then check second and second last and so on. for this u can create a loop-:
    for(i=0,j=str.lenght()-1,s=1;i!=j;i++,j--)
    {
    if(a[i]!=a[j])
    {
    s=2;
    break;
    }
    }
    if the value of s remains 1 then it is a pelindrome otherwise not
    Last edited by vedzz; 10-29-2009 at 05:05 PM.

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    This has been discussed to death. I am closing this thread.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •