Q #1) Write a Java Program to reverse a string without using String inbuilt function reverse().
Answer:
Method 1:
There are several ways with which you can reverse your string if you are allowed to use the other string inbuilt functions.
In this method, we are initializing a string variable called str with the value of your given
string. Then, we are converting that string into character array with toCharArray() function. Thereafter, we are using for loop to iterate between each character in reverse order and printing each character.
1 | public class FinalReverseWithoutUsingInbuiltFunction { |
2 | public static void main(String[] args) { |
3 | String str = "Saket Saurav" ; |
4 | char chars[] = str.toCharArray(); |
5 | for ( int i= chars.length- 1 ; i>= 0 ; i--) { |
6 | System.out.print(chars[i]); |
Output:
varuaS tekaS
No comments:
Post a Comment