English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

4 answers

Logic:

1. Put the original string into an array (in your example, this would be 890).
2. Determine the length of the array.,
3. Loop around the array starting at length-1 and decrementing down to zero (arrays are zero indexing !).
4. Retrieve the element at the current array's index
5. Write it to another array,
6. Increment the second arrays index
7. Decrement the index of the first array.
8. When you have completed reading all th elements from the first array, convert the 2nd array back to a string.

If the original value was a number, parse the new string into an integer.

Hope this helps

2007-11-11 20:02:26 · answer #1 · answered by jools 3 · 0 0

Equivalent value of number
980 is 89 not 089
so it might be
1) string
then just reverse it

2)if it is char array
then

find the length
make var2=0

for var=length-1 to 0
new-array[var2++]=array[var];


when u come out of he loop u must have the reverse of the array that is 890 to 098 in new-array.

2007-11-12 04:16:23 · answer #2 · answered by purna 1 · 0 0

public class swap
{
public static void main(String [] args)
{
String s1 = "890";
String s2;
for (int i=s1.length-1; i>=0; i--)
{
s2 =+ s1.charAt(i);
}
System.out.println(s2);
}
}

I haven't compiled or tested it or anything - it might have an error or two.

Edit: No, purna. You can simply call Integer.parseInt("082") and it will happily process it and return 82. Messing around with index's and looking for 0's and all that gets too confusing, I think.

Here is code that compiles and runs:

public class swap
{
public static void main(String [] args)
{
String s1 = "890";
String s2 = "";
for (int i=s1.length()-1; i>=0; i--)
{
s2 += s1.charAt(i);
}
System.out.println(s2);
int i1 = Integer.parseInt(s2);
System.out.println(i1);
}
}

2007-11-12 04:04:58 · answer #3 · answered by Anonymous · 0 0

Reversing any kind of testing text?

Use this code if you are in introductory computer science, recursion topic:

public static String rev(String text) {
if (text.length() == 0) return "";
else return (rev(text.substring(1)) + text.charAt(0));
}

2007-11-12 05:00:27 · answer #4 · answered by Andy T 7 · 0 0

fedest.com, questions and answers