Hi All,
Hi All, Here I write programs for the given String Reverse functionality in both manual and Java inbuilt functionality in the same way in Digits (Numbers) also :-)
import java.util.Scanner;
public class reversenum {
public static void main(String args[]) {
//Reverse Integer Digits in Java
System.out.println("Reverse Integer Digits in Java");
System.out.println("Enter the digits for reverse ");
Scanner s = new Scanner(System.in);
int i = reverseDigt(s.nextInt());
System.out.println("After Reverse given digits :-) ");
System.out.println(i);
//Reverse String in Java
System.out.println("Using String functioality Reverse the String in Java ");
System.out.println("Please enter any content ");
Scanner str1 = new Scanner(System.in);
String name = str1.nextLine(); // reading a string from console
String reverse1 = new StringBuffer(name).reverse().toString(); //Using inbuild reverse function example
System.out.printf("Original String : %s , Reversed String %s %n", name, reverse1);
//manually reverse function example
System.out.println("Using manually string reverse functionlity in Java ");
Scanner str2 = new Scanner(System.in);
String name1 = str2.nextLine(); // reading a string from console
reverse1 = srtreverse(name1);
System.out.printf("Original String : %s , Reversed String %s %n", name1, reverse1);
}
// function for manually reverse the String
public static String srtreverse(String source) {
if (source == null || source.isEmpty()) {
return source;
}
String reverse = "";
for (int i = source.length() - 1; i >= 0; i--) {
reverse = reverse + source.charAt(i);
}
return reverse;
}
// function for manually reverse the digits
public static int reverseDigt(int x) {
String st = x + "";
StringBuffer sb = new StringBuffer(st).reverse();
return new Integer(sb.toString()).intValue();
}
}
output:
~~~~~~
Reverse Integer Digits in JavaEnter the digits for reverse
123456
After Reverse given digits :-)
654321
Using String functioality Reverse the String in Java
Please enter any content
skitech
Original String : skitech , Reversed String hcetiks
Using manually string reverse functionlity in Java
telent
Original String : telent , Reversed String tnelet
No comments:
Post a Comment