Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions JAVA/reverseString.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
public class reverseString
{
public static void main(String[] args) {
String string = "Dream big";

String reversedStr = "";


for(int i = string.length()-1; i >= 0; i--){
reversedStr = reversedStr + string.charAt(i);
}

System.out.println("Original string: " + string);

System.out.println("Reverse of given string: " + reversedStr);
}
}
public class reverseString {
public static String reverse(String str) {
// converting str to char array for easy traversal
char[] chars = str.toCharArray();
//pointing one pointer to left
int left = 0;
//pointing another pointer to right
int right = chars.length - 1;
// moving till left and right not crosses each other
while (left < right) {
// swaping of left and right to reverse a string
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}

return new String(chars);
}

public static void main(String args[]){
String str="Dream big"; // Changed the input string
//calling the function which will return a string so we are printing that
System.out.println(reverse(str));
}
}