C++ Riddle
Results 1 to 4 of 4

Thread: C++ Riddle

  1. #1
    Junior Member
    Join Date
    Oct 2003
    Posts
    9

    C++ Riddle

    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?
    |The Spider Sense|

  2. #2
    AntiOnline Senior Member souleman's Avatar
    Join Date
    Oct 2001
    Location
    Flint, MI
    Posts
    2,884
    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.
    \"Ignorance is bliss....
    but only for your enemy\"
    -- souleman

  3. #3
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Souleman is on the right path...
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  4. #4
    Member
    Join Date
    Oct 2003
    Posts
    78
    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:

    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();
    			}
        }
    }
    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...

    If it has to be in C++, drop me a line, I still remember of some of that stuff...
    [pong][shadow]Why won\'t anyone give me greenies???[/shadow] [/pong]

Posting Permissions

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