CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Reversing letters of a words in a sentence keeping the words in the same order

    Code:
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to the word reversing program");
            System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
            System.out.print("Please enter a Sentence: ");
            String original = input.nextLine();
            wordReverse(original);
            String reverse = wordReverse(original);
            fixOrder(reverse);
            String[] newSentence = fixOrder(reverse);
            System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence );
        }
        
        public static String wordReverse(String original){
    
            StringTokenizer string = new StringTokenizer(original);
            
            Stack<Character> charStack = new Stack<Character>();
    
            while (string.hasMoreTokens()){
    
            String stack = string.nextToken();
    
            for (int i = 0; i < stack.length(); i ++){
    
            charStack.push(stack.charAt(i));
        }
            charStack.push(' ');
        }
    
            StringBuilder result = new StringBuilder();
            while(!charStack.empty()){
            result.append(charStack.pop());
        }
    
            return result.toString();   
        }
     
        public static String[] fixOrder(String reverse){
           
           String arrayList[] = {reverse};
            String[] result = arrayList;
           for (int index = arrayList.length-1; index >= 0; index--)
                result = arrayList;
           
           
           return result;
        }
    }
    when i run the program it states: Reversing the words in the sentence: hello world, looks like: [Ljava.lang.String;@5304f889

    so if a user types: I am enjoying this the output would be i ma gniyojne siht

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Reversing letters of a words in a sentence keeping the words in the same order

    Hi,

    Code:
    System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence );
    newSentence is an array of Strings. It doesn't use toString() method of String class to print the output. It uses toString() method of Object class, which prints the class name followed by its hash value. That's why you get [Ljava.lang.String;@5304f889.

    In your case, newSentence is a 1-length array, so you can prints its contents doing:
    Code:
    System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence[0] );
    Anyway, it won't print what you want, because you are reversing the whole input sentence, not word by word, as I think you want. By the way, what are you attempting to do in fixOrder() method? I guess it does nothing, unless you want to convert an string to a 1-length string array.

    Hint: You can use Stack class to reverse String (although StringBuilder/StringBuffer both have their own version of 'reverse'). Regardless of the method you use, first split each word of input sentence and then reverse only that word. Finally, build again the whole sentence. You'll have to make just a little modification in your code. More precisely, in wordReverse() function.

    Albert.
    Last edited by AlbertGM; July 5th, 2012 at 08:24 AM.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    Dec 2012
    Posts
    1

    Thumbs up Re: Reversing letters of a words in a sentence keeping the words in the same order

    <HTML>
    <HEAD>
    <SCRIPT>

    var ss="i am bala";
    var ss8=ss.length;

    var ss1="";
    ss1=ss.split(" ");

    for(i=0;i<ss1.length;i++)
    {
    ss2=ss1[i];
    ss3=ss2.split("");
    ss3.reverse();
    ss1[i]=ss3.join("");
    }

    document.write(ss1.join(" "));

    </SCRIPT>
    </HEAD>

    <BODY>

    </BODY>

    </HTML>

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Reversing letters of a words in a sentence keeping the words in the same order

    Thanks for posting how to do it using JavaScript, unfortunately this is a Java forum.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 2004
    Posts
    102

    Re: Reversing letters of a words in a sentence keeping the words in the same order

    Another option would be to tokenize your input string and send each token to a reverse method. The reverse method would be called recursively using the Java API substring as input to the reverse method. The reverse method would initially print out the last char and recursively use substring to print the next to last char etc.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured