cstring-palindrom-array problem....

it returns "is palindrom" for everything....

Page 1 of 1

6 Replies - 3850 Views - Last Post: 13 February 2009 - 11:56 AM Rate Topic: -----

#1 OrangeSection   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

cstring-palindrom-array problem....

Post icon  Posted 13 February 2009 - 09:29 AM

This code dont work as it should, and i canīt find the problem with it. There is no errors.
It executes ok, but every string i add returns that itīs a palindrome even if it isnīt.....

Hereīs the code :

#include<iostream>
#include<cstring>
using namespace std;

bool palindrome (char palindrome [80]);

int main()
{
	//Declarations.
	const int size = 80;
	char palindromestring[size];

	//Prompt the user to enter a line of text.
	cout << "Please, enter a line of text: ";

	cin >> palindromestring; //Read the line of text.

	cout << endl << endl; // Makes the output more clean.

	palindrome (palindromestring);
	if (true)
	{
		cout << "\n";
		cout << "is palindrom\n\n";
	}
	else
	{
		cout << "\n";
		cout << "no palindrom\n\n";
	}
return 0;
}

bool palindrome(char palindromestring[80])
{
	int left = 0; //startindex from left
	int right = strlen(palindromestring) - 1; //startindex from right
	while (left < right)
	{
		   //skip spaces
		   while (palindromestring[left] == ' ')
		   ++left;
		   while (right > left && palindromestring[right] == ' ') //keep to the right of left
		   --right;
		   if (tolower(palindromestring[left]) != tolower(palindromestring[right])) //if different => no palindrom
		  {
				return false;
		  }
		   ++left;
		   --right;
	  }
   return true;
}




I could really use another pair of eyes here.....mine are done for ;)

Is This A Good Question/Topic? 0
  • +

Replies To: cstring-palindrom-array problem....

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 09:43 AM

Ummm...
palindrome (palindromestring);
if (true)
{
    cout << "\n";
    cout << "is palindrom\n\n";
}


Don't you mean:
if (palindrome (palindromestring))
{
    cout << "\n";
    cout << "is palindrom\n\n";
}

Was This Post Helpful? 1
  • +
  • -

#3 OrangeSection   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 09:54 AM

View PostJackOfAllTrades, on 13 Feb, 2009 - 08:43 AM, said:

Ummm...
palindrome (palindromestring);
if (true)
{
    cout << "\n";
    cout << "is palindrom\n\n";
}


Don't you mean:
if (palindrome (palindromestring))
{
    cout << "\n";
    cout << "is palindrom\n\n";
}

But, thats my call to the function "bool palindrome"... what do you mean? :blink:
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 10:07 AM

true will always evaluate to true.

You need to get the return value from your function...exactly as has been shown. In your code, you call the function, and do not store the return value, so it is never evaluated. In Jack's code, he is actually using the return value.
Was This Post Helpful? 1
  • +
  • -

#5 OrangeSection   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 10:12 AM

View PostAmadeus, on 13 Feb, 2009 - 09:07 AM, said:

true will always evaluate to true.

You need to get the return value from your function...exactly as has been shown. In your code, you call the function, and do not store the return value, so it is never evaluated. In Jack's code, he is actually using the return value.

ah yes! of course! Sometimes you think "this" and you code "that". Kinda stupid of me......

Thanks for your help!
The first friday beer is early today, it seems...... :D
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 10:15 AM

Glad it's working:), although the thanks should go to Jack :)
Was This Post Helpful? 0
  • +
  • -

#7 OrangeSection   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: cstring-palindrom-array problem....

Posted 13 February 2009 - 11:56 AM

View PostAmadeus, on 13 Feb, 2009 - 09:15 AM, said:

Glad it's working:), although the thanks should go to Jack :)


i thanked u both, he coded, u explained :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1