how to reverse word by word?
:s: how would you reverse a word of a string sentence "hi to all?" without using loops?
basically we are not using any "for,while,do or if, if else". the only methods we are using is the class strings methods.
could someone please guide me to the right direction?
could you provide me the simplest program?
thank you.
Re: how to reverse word by word?
Quote:
Originally Posted by
yamicaitsith
:s: how would you reverse a word of a string sentence "hi to all?" without using loops?
basically we are not using any "for,while,do or if, if else". the only methods we are using is the class strings methods.
What is your exact instructions, word for word? Can you please post this?
Quote:
could someone please guide me to the right direction?
Let's first find out more about the problem and exactly where you're stuck.
Quote:
could you provide me the simplest program?
No, we should not provide solutions for homework problems as this is unethical and even worse cheats you out of a valuable learning experience. Again, let's find out more about the problem and exactly where you sit, and see if we can help *you* to solve this and come up with the best program/solution.
Re: how to reverse word by word?
public static void main(String[] args) {
// TODO Auto-generated method stub
String sentence = "Alienware Lair";
System.out.println(sentence);
int n = sentence.length();
System.out.println(n);
int position = sentence();
System.out.println("the word \"Lair\" starts at index" + position);
n = sentence.substring(0, position + "Alienware");
String reverse = new StringBuffer("Alienware Lair").reverse().toString();
String string= "Alienware Lair";
System.out.println("String before reverse: "+ string);
System.out.println("String after reverse: "+ reverse);
}
}
Re: how to reverse word by word?
im trying to figure out how to change the position from "Alienware Lair" as >>>>>> " Lair Alienware",
i dont need the solution only what method i should use and if u can provide a example. doesn't has to be like i said the solution?.
and ill be sincere as it is for a lab job.
ik very little programming and any help on how to use these methods would be useful.
thank you.
Re: how to reverse word by word?
String (Java 2 Platform SE v1.4.2)
ive been looking for hours on the web, but honestly i dont understand most of it.
Re: how to reverse word by word?
i got my chars to reversed and yet the method for changing my sentence hasnt been resolved which is mainly the last thing i need to do
String sentence = "video games";
System.out.println(sentence);
sentence= sentence.toUpperCase();
System.out.println(sentence);
int n = sentence.length();
System.out.println(n);
String reverse = new StringBuffer("video games").reverse().toString();
String string= "video games";
System.out.println("String before reverse: "+ string);
System.out.println("String after reverse: "+ reverse);
sentence = sentence.substring(7,10);
System.out.println(sentence);
}
}
** i changed the values of this program due to be a lab assignment, its not the original ones i had at the time.
Re: how to reverse word by word?
BB Code List - Java Programming Forum
Please edit your posts and add the tags.
db
Re: how to reverse word by word?
You can use the String method split(" ") to split your String using the spaces as delimiters producing an array of words. But then you'd still need a for loop to iterate backwards through the array. Again, I suggest that you post your actual instructions, as it will be hard for us to help without knowing exactly what your restrictions are.
Also, as Darryl suggests, please edit your posts above and give your posted code proper code tags so that we may be able to read the code. His links will help.
Re: how to reverse word by word?
Recursion down the String[] from a split() call?
Re: how to reverse word by word?
ok.:(whew): after analyzing this issue i finally got it solved...
i will refresh the tags and post my example here for future aid on anyone who might use it for modifying new solutions.
i want to thank you all moderators for your help :).
i might be a newbi in java, but hopefully get to known more during my programming and game design career.
thank u again yami.
Re: how to reverse word by word?
String s1 = "Video";
String s2 = "games";
String s3 = s2 + s1;
s3 = s2 + " " + s1;
System.out.println(s3);
Re: how to reverse word by word?
Quote:
Originally Posted by
yamicaitsith
String s1 = "Video";
String s2 = "games";
String s3 = s2 + s1;
s3 = s2 + " " + s1;
System.out.println(s3);
I can't decide whether or not you are serious in posting this trivia?
You should note that in conjunction with recursion, String.indexOf() and String.substring(), this can be used as the basis for a general method for reversing the words of a String. The body of a general method to perform the reverse is just two lines of code.
Re: how to reverse word by word?
Well obviously idk what would you do? If you didn't know about loops and/or was aloud to use em on a project?.
This matter could be discussed endlessly and I'm sure it could of have better solutions.
This was what I figured out and worked properly, anything after that I'm sure it can be done depending on what you decide to edit your program.
Re: how to reverse word by word?
Quote:
Originally Posted by
yamicaitsith
Well obviously idk what would you do? If you didn't know about loops and/or was aloud to use em on a project?.
This matter could be discussed endlessly and I'm sure it could of have better solutions.
This was what I figured out and worked properly, anything after that I'm sure it can be done depending on what you decide to edit your program.
:-) Brilliant! I'm mystified.