IF I input a string say: "He is good"
the out will be: "eH si doog"
I can only functions such a : charAT(int) ; length()
The program should be done using for loop. I am confused.Please Help:confused:
Printable View
IF I input a string say: "He is good"
the out will be: "eH si doog"
I can only functions such a : charAT(int) ; length()
The program should be done using for loop. I am confused.Please Help:confused:
one way:
take the string as character array
c[]=[h,e, ,i,s, ,g,o,o,d]
now read each character from end concat each character into a string.
String s=new String();
s.concat(c[end]);
second way:
in StringBuffer there is method called reverse()
by using this we can reverse the string
i hope you can understand:rolleyes:
here's another way:
Code:String str = "he is good";
String[] temp;
String reversedStr = "";
String delimiter = " ";
temp = str.split(delimiter);
// temp [string array] now stores three values, "he", "is", and "good"
for (String s:temp) {
reversedStr += new StringBuffer(s).reverse().toString() + delimiter;
}
// reversedStr = "eh si doog";
I cannot use array too or any other functions that i did not mention.
oh you have to do it manually i see. then you need to work out your logic
- while (index doesnt equal length of string) {
- X = start index;
- if charAt(index) = white space {
index X to here = new word
reverse it
}
- loop }
"length of string"
use String.length
"index X to here"
when you have two indexes e.g. 0 and 2, you can use subsequence to get all the characters between two indexes
"reverse it"
take the last index first and copy it to a temporary variable, loop through the sub-word until the length of it is finished
Can you give a sample code please...
i would except this looks like your homework :). you wont learn unless you try it yourself, so i've given u some logic to help u out. try to write it and try to compile it, if you get an error tell us what the error is
Code:String sample="he is good";
for(int x=sample.length()-1;x>=0;x--){
System.out.print(sample.charAt(x));
}
What have you tried to solve this? You got advice in this thread, some incorrect as pbrock mentioned but they are showing the idea. Show what kind have work you have done and we will let you know what to focus on.
Please don't spoon feed code. Especially when the code you are giving is incorrect, and has already been provided, and the reason why it was wrong has been explained.
Please compile, run and test any code before posting. That'll save you from the embarrassment of posting erroneous code that doesn't come close to meeting the requirement.
And when you do manage to develop a code that works, don't spoonfeed -- that deprives the questioner of a learning opportunity.
db
the result should be: "eH si doog"
not : "doog si eH"
Also, don't spoonfeed.
Sorry guys I misread OP's post and ill try not to spoonfeed.