hngmin15 Posted October 18, 2004 Share Posted October 18, 2004 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 More sharing options...
0 Glatz Posted October 18, 2004 Share Posted October 18, 2004 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 More sharing options...
0 John Veteran Posted October 18, 2004 Veteran Share Posted October 18, 2004 // 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 More sharing options...
0 lime Posted October 18, 2004 Share Posted October 18, 2004 i wrote this exact same thing as part of an assignment while i was at KU.....i can dig it up and post it if you want... Link to comment Share on other sites More sharing options...
0 hngmin15 Posted October 19, 2004 Author Share Posted October 19, 2004 is it name.length()? Link to comment Share on other sites More sharing options...
0 hngmin15 Posted October 19, 2004 Author Share Posted October 19, 2004 // 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 More sharing options...
0 John Veteran Posted October 19, 2004 Veteran Share Posted October 19, 2004 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 More sharing options...
0 kjordan2001 Posted October 19, 2004 Share Posted October 19, 2004 myString.length() if you're using a string, strlen(myString) if you're using char*. Link to comment Share on other sites More sharing options...
0 mrvc Posted October 19, 2004 Share Posted October 19, 2004 // 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 More sharing options...
0 John Veteran Posted October 19, 2004 Veteran Share Posted October 19, 2004 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 More sharing options...
Question
hngmin15
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