• 0

another c++ program help


Question

hi, does anyone know if you can do palindromes in c++ using loops?

it starts by asking "enter a word" then when you enter a word it tells you if its a palindrome or not.please help me with this if you know

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Well you could have a program that gives you the reverse, but the computer won't know if it's a word.

You could try getting a large list and checking the word against the list.

Link to comment
Share on other sites

  • 0

// C#
bool isPalindrome = true;
for(int i = 0; i < myString.Length / 2; i++)
{
 ? ?if(myString[i] != myString[myString.Length - i])
 ? ?{
 ? ? ? ?isPalindrome = false;
 ? ?}
}

I haven't tested this, but it should work. The c++ code should be almost identical, but you'll have to figure out the equivalent of myString.Length; I've never worked with the c++ string class.

Link to comment
Share on other sites

  • 0
// C#
bool isPalindrome = true;
for(int i = 0; i < myString.Length / 2; i++)
{
 ? ?if(myString[i] != myString[myString.Length - i])
 ? ?{
 ? ? ? ?isPalindrome = false;
 ? ?}
}

I haven't tested this, but it should work. The c++ code should be almost identical, but you'll have to figure out the equivalent of myString.Length; I've never worked with the c++ string class.

584761924[/snapback]

this code does not work it it saids when i run it program abort.

Link to comment
Share on other sites

  • 0

No, the code I posted is in C#, not c++. I'm pretty sure the c++ string class doesn't have a .Length property (hell, c++ doesn't even have properties...) so I just meant for you to use it as a guideline.

Link to comment
Share on other sites

  • 0
// C#
bool isPalindrome = true;
for(int i = 0; i < myString.Length / 2; i++)
{
 ? ?if(myString[i] != myString[myString.Length - i])
 ? ?{
 ? ? ? ?isPalindrome = false;
 ? ?}
}

I haven't tested this, but it should work. The c++ code should be almost identical, but you'll have to figure out the equivalent of myString.Length; I've never worked with the c++ string class.

584761924[/snapback]

Shouldn't the condition be myString != myString[myString.Length - i - 1] since the last char of a string is at (length - 1)?

Link to comment
Share on other sites

  • 0
Shouldn't the condition be myString != myString[myString.Length - i - 1] since the last char of a string is at (length - 1)?

584764312[/snapback]

Yes.

I told you I didn't test it! :p

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.