Task - String Programs - Reverse a String #39
Replies: 31 comments
|
|
|
|
|
`package Week4; import java.util.Scanner; public class ReverseString { } |
|
|
import java.util.Scanner; } |
|
|
|
|
import java.util.Scanner; |
|
public class reversestr { } |
|
|
|
class Rev |
|
`import java.util.Scanner; } |
|
`package all_strings; import java.util.*; public class ReverseStringWithSpaces { } |
|
|
|
|
|
`public class ReverseString { }` |
|
|
|
`public class StringreverseWord { } |
|
` }` |
|
public class Stringreverse { } |
|
public class ReverseString { } |
|
import java.util.*;
public class ReverseString {
public static String reverse(String s) {
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString() + " ";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb1 = new StringBuilder();
System.out.println("Enter the string:");
String str = sc.nextLine();
int n = str.length() - 1;
String[] parts = str.split(" ");
System.out.println("rversed string:");
for (int i = parts.length - 1; i > -1; --i) {
sb1.append(reverse(parts[i]));
}
String newstr = sb1.toString();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
System.out.print(" " + newstr.charAt(i));
} else
System.out.print(newstr.charAt(i));
}
}
} |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Write a Java program to reverse a given string preserving the position of spaces.
Example:
All reactions