Method 1:
1 | public class FinalReverseWithoutUsingInbuiltFunction { |
2 | public static void main(String[] args) { |
3 | String str = "Saket Saurav" ; |
4 | char chars[] = str.toCharArray(); // converted to character array and printed in reverse order |
5 | for ( int i= chars.length- 1 ; i>= 0 ; i--) { |
6 | System.out.print(chars[i]); |
7 | } |
8 | } |
9 | } |
Output:
varuaS tekaS
Method 2:
1 | import java.util.Scanner; |
2 |
3 | public class ReverseSplit { |
4 |
5 | public static void main(String[] args) { |
6 | // TODO Auto-generated method stub |
7 | String str; |
8 | Scanner in = new Scanner(System.in); |
9 | System.out.println( "Enter your String" ); |
10 | str = in.nextLine(); |
11 | String[] token = str.split( "" ); //used split method to print in reverse order |
12 | for ( int i=token.length- 1 ; i>= 0 ; i--) |
13 | { |
14 | System.out.print(token[i] + "" ); |
15 | } |
16 | |
17 | } |
18 |
19 | } |
Output:
Enter your String
Softwaretestinghelp
plehgnitseterawtfoS
Softwaretestinghelp
plehgnitseterawtfoS