This Java code will take the Input String from Console by the user and prints the reversed string into the Console.
input: qwerty
output: ytrewq
import java.util.Scanner;
public class Test {
public static String reverseString(String value) {
char[] valueList = value.toCharArray();
int j = 0;
for (int i = value.length()-1; i>=0; i--) {
valueList[j] = value.charAt(i);
j++;
}
value = String.valueOf(valueList);
return value;
}
public static void main(String[] args) {
String value ;
Scanner input = new Scanner (System.in);
System.out.println ("Enter String Value to be reversed: ");
value = input.nextLine();
String reversedString = reverseString(value);
System.out.println("Reverse2 String is : " + reversedString);
}
}
input: qwerty
output: ytrewq
import java.util.Scanner;
public class Test {
public static String reverseString(String value) {
char[] valueList = value.toCharArray();
int j = 0;
for (int i = value.length()-1; i>=0; i--) {
valueList[j] = value.charAt(i);
j++;
}
value = String.valueOf(valueList);
return value;
}
public static void main(String[] args) {
String value ;
Scanner input = new Scanner (System.in);
System.out.println ("Enter String Value to be reversed: ");
value = input.nextLine();
String reversedString = reverseString(value);
System.out.println("Reverse2 String is : " + reversedString);
}
}
No comments:
Post a Comment