Task - String Programs - Reverse a String #39
Replies: 31 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package Week4; import java.util.Scanner; public class ReverseString { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; |
Beta Was this translation helpful? Give feedback.
-
|
public class reversestr { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
class Rev |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; } |
Beta Was this translation helpful? Give feedback.
-
|
`package all_strings; import java.util.*; public class ReverseStringWithSpaces { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`public class ReverseString { }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`public class StringreverseWord { } |
Beta Was this translation helpful? Give feedback.
-
|
` }` |
Beta Was this translation helpful? Give feedback.
-
|
public class Stringreverse { } |
Beta Was this translation helpful? Give feedback.
-
|
public class ReverseString { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
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));
}
}
} |
Beta Was this translation helpful? Give feedback.
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:
Beta Was this translation helpful? Give feedback.
All reactions