Results 1 to 11 of 11
- 04-23-2011, 12:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
help with returning the string in reverse
I'm somewhat new to java and I'm trying to get a string to return in reverse which takes a String parameter, str, for example the word "hello", I want to be able to output "olleh" the code I used was :
String result = " ";
for (int k = 0; k < string.length; k++);
result(length-1-i) = str(i);
return result;
I'm not getting the output I want so if someone can help me with this I'll really appreciate it.
-
you ended your for loop without doing anything by adding a semi-colon at the end. remove the semi-colon and use braces like this:
Java Code:for (int k=0; k<string.length; k++) { result(length-1-i) = str(i); return result; }
i'm assuming you declared 'i' and the method result(int) somewhere else
- 04-23-2011, 12:51 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
Yea, it starts like this but I need to declare i or can I replace i with k?
public String reverseString(String str)
for (int k=0; k<string.length; k++)
result(length-1-i) = str(i);
return result;
- 04-23-2011, 02:14 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
just use the String.reverse() method in the String class
- 04-23-2011, 02:35 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
so I dont have to type in that for loop?
- 04-23-2011, 08:28 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
yes so that your life would be easier
- 04-23-2011, 01:37 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 04-23-2011, 03:14 PM #8
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
/*
Java String Reverse example.
This example shows how to reverse a given string
*/
public class StringReverseExample {
public static void main(String args[]){
//declare orinial string
String strOriginal = "Hello World";
System.out.println("Original String : " + strOriginal);
/*
The easiest way to reverse a given string is to use reverse()
method of java StringBuffer class.
reverse() method returns the StringBuffer object so we need to
cast it back to String using toString() method of StringBuffer
*/
strOriginal = new StringBuffer(strOriginal).reverse().toString();
System.out.println("Reversed String : " + strOriginal);
}
}
/*
Output of the program would be :
Original String : Hello World
Reversed String : dlroW olleH
*/
- 04-23-2011, 03:26 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Hurray! Spoonfeeding again.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-23-2011, 05:28 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
This code here works great in any situation.
public String reverse(String str) {
char[] sequence = str.toCharArray();
char[] reversed = new char[sequence.length];
int j = 0;
for (int i = sequence.length - 1; i >= 0; i--) {
reversed[j] = sequence[i];
j++;
}
String returnString = new String(reversed);
return returnString;
}
}
-
Similar Threads
-
How to reverse a string?
By Neeer in forum New To JavaReplies: 17Last Post: 03-27-2011, 08:10 PM -
Reverse A String Without Allocating A New String
By marco.c84 in forum Advanced JavaReplies: 10Last Post: 03-22-2011, 05:39 AM -
Reverse a string?
By cysquatch in forum New To JavaReplies: 15Last Post: 03-23-2010, 02:31 AM -
reverse string split
By Fittersman in forum Advanced JavaReplies: 4Last Post: 03-09-2010, 12:29 AM -
reverse a string with a while loop...
By OptimusPrime in forum New To JavaReplies: 9Last Post: 12-28-2009, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks