Can anyone write a C++ program which asks the user to input a string and checks if any word in the string is a palindrome.....
eg... if the string is Dad i am riding kayak, the program must display...
Dad
Kayak
each word remember!!
?anticode?
Printable View
Can anyone write a C++ program which asks the user to input a string and checks if any word in the string is a palindrome.....
eg... if the string is Dad i am riding kayak, the program must display...
Dad
Kayak
each word remember!!
?anticode?
I can't program any more, its been a long time, but it shouldn't be to hard to do. Just do something like....
while (more words)
cin << 1word
drow1 = reverse of word1
compare 1word & drow1
if same, print, else, goto next word.
Souleman is on the right path...
If you want one in Java, I'll be glad to edit my normal palindrome tester which I did for an AP Java class, now it tests entire sentences, here's the source below:
I know I have a lot of useless classes being imported, but since I use an outline for every program thats been premade, I really don't delete any...Code:/*
Dave T
Date: September 30, 2003
Page: 184 Problem 3.12
The Quote of the Day:
"Eat right, don't smoke, die anyway."
*/
import cs1.Keyboard;
import java.util.Random;
import java.util.StringTokenizer;
import java.text.NumberFormat;
public class PalindromeTester
{
public static void main (String[] args)
{
String str, another = "y", lower;
int left, right;
while (another.equalsIgnoreCase("y"))
{
System.out.println ("Enter a potential palindrome:");
str = Keyboard.readString();
lower = str.toLowerCase();
left = 0;
right = str.length() - 1;
while (lower.charAt(left) == ' ' || lower.charAt(left) == '.' || lower.charAt(left) == ',' || lower.charAt(left) == '?')
{left++;}
while (lower.charAt(right) == ' ' || lower.charAt(right) == '.' || lower.charAt(right) == ',' || lower.charAt(right) == '?')
{right--;}
while (lower.charAt(left) == lower.charAt(right) && left <= right)
{
left++;
right--;
while (lower.charAt(left) == ' ' || lower.charAt(left) == '.' || lower.charAt(left) == ',' || lower.charAt(left) == '?')
{left++;}
while (lower.charAt(right) == ' ' || lower.charAt(right) == '.' || lower.charAt(right) == ',' || lower.charAt(right) == '?')
{right--;}
}
System.out.println();
if (left < right)
System.out.println("That string is NOT a palindrome.");
else
System.out.println("That string IS a palindrome.");
System.out.println();
System.out.print("Test another palindrome (y/n)? ");
another = Keyboard.readString();
}
}
}
If it has to be in C++, drop me a line, I still remember of some of that stuff...