-
July 4th, 2012, 04:11 PM
#1
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
-
July 5th, 2012, 03:17 AM
#2
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 :-)
-
December 5th, 2012, 07:11 AM
#3
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>
-
December 7th, 2012, 10:56 AM
#4
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.
-
December 10th, 2012, 11:40 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|