Skip to content

Commit 4d34882

Browse files
Merge pull request #46 from darshanr27/master
Solution for String Reverse
2 parents edd69bd + 8d190a5 commit 4d34882

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

String Handling/StringReverse.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Write a java program to reverse the string without using the inbuilt function.
2+
3+
import java.util.Scanner;
4+
5+
class StringReverse {
6+
public static void main(String [] args)
7+
{
8+
Scanner scan = new Scanner(System.in);
9+
System.out.println("Enter a String:");
10+
String str = scan.nextLine();
11+
System.out.println("-----------------------");
12+
System.out.println("Sting Before Reversing:");
13+
System.out.println(str);
14+
15+
// Converting the string into character array
16+
char arr1[] = str.toCharArray();
17+
18+
// Creating the new array to store the character while reversing
19+
char arr2[] = new char[arr1.length];
20+
21+
// The j value will be used to start storing the character from last index in arr2
22+
int j = arr2.length - 1;
23+
24+
// Using for loop to traverse through the arr1
25+
// In each iteration the character in arr1 is stored in the arr2 from last index in each iteration the j value decrements
26+
for (int i = 0; i <= arr1.length-1; i++) {
27+
arr2[j] = arr1[i];
28+
j--;
29+
}
30+
31+
// Creating the string using the character array arr2
32+
str = new String(arr2);
33+
System.out.println("-----------------------");
34+
System.out.println("String After Reversing:");
35+
System.out.println(str);
36+
}
37+
}

0 commit comments

Comments
 (0)