GeeksforGeeks
geeksforgeeks.org › dsa › reverse-a-string
Reverse a String – Complete Tutorial - GeeksforGeeks
// C program to reverse a string using two pointers #include <stdio.h> #include <string.h> char* reverseString(char *s) { int left = 0, right = strlen(s) - 1; // Swap characters from both ends till we reach // the middle of the string while (left < right) { char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } return s; } int main() { char s[] = "abdcfe"; printf("%s", reverseString(s)); return 0; } ... // Java program to reverse a string using two pointers class GfG { static String reverseString(String s) { int left = 0, right = s.length() - 1; // Use StringBuilder for m
Published October 3, 2025
Java67
java67.com › 2016 › 06 › how-to-reverse-string-in-place-in-java.html
[Solved] How to reverse a String in place in Java? Example | Java67
It will also make you a better programmer because you know how to solve an unseen problem using existing patterns. That's all about how to reverse String in place in Java. This is a common algorithm that uses two pointer approach. Since it requires us to traverse the array till the middle, the time complexity is O(n/2) i.e.
Videos
01:41
Reverse String | Leetcode 344 | Two Pointer Strings 🔥🔥 | Live ...
04:00
Reverse String (Two Pointers Approach ) | Leet Code - YouTube
04:18
Reverse a String in Java | Reverse String LeetCode Solution
03:18
Reverse String | Leetcode #344 ( Two pointer approach ) - YouTube
31:23
DAY 154 - Remove and Reverse | Two Pointers, Strings | JAVA | C++ ...
What is a String in Java?
Strings in Java are immutable (cannot be changed) non-primitive datatype that stores a sequence or array of characters.
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › reverse a string in java
Reverse a String in Java - Shiksha Online
What is Stack in Java?
Stack is a data structure in java that follows the last-in-first-out (LIFO) concept.
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › reverse a string in java
Reverse a String in Java - Shiksha Online
What are Lists in Java?
Java has ArrayLists which are arrays that resize themselves as needed. A typical implementation is when the array is full, the array doubles in size.
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › reverse a string in java
Reverse a String in Java - Shiksha Online
Medium
medium.com › @YodgorbekKomilo › two-pointer-technique-in-java-58542f961ac8
Two-Pointer Technique in Java. The two-pointer technique in Java… | by Yodgorbek Komilov | Medium
September 30, 2024 - public class ReverseString { public static String reverse(String s) { char[] chars = s.toCharArray(); int left = 0, right = chars.length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } return new String(chars); } public static void main(String[] args) { String s = "Hello, World!"; String reversed = reverse(s); System.out.println(reversed); // Output: !dlroW ,olleH } } This example merges two sorted arrays into one sorted array. import java.util.Arrays; public class MergeSortedArrays { public static int[] merge(int[] arr1,
The Knowledge Academy
theknowledgeacademy.com › blog › reverse-a-string-in-java
Reverse a String in Java? - A Complete Guide
Two pointers, 'left' and 'right', are initialized to point to the first and last characters of the Java Array, respectively. In the while loop, the characters pointed to by 'left' and 'right' are interchanged using a temporary variable, 'temp', and the pointers are moved towards each other until they meet at the middle of the array. To reverse a string using the creation of a new string, firstly, you must create an empty string to store the reversed string.
Emeritus
emeritus.org › home › blog › information technology › 9 most convenient ways to reverse a string in java
9 Most Convenient Ways to Reverse a String in Java
September 24, 2024 - In the above example, the ‘toCharArray()’ method converts the input string into a character array. Then two pointers named ‘left’ and ‘right’ point to the first and last character of the array respectively. Next, the ‘left’ and ‘right’ pointer exchanges their pointed characters using a temporary variable ‘temp’ in the while loop. Lastly, the code prints the original and reversed string using the ‘System.out.printIn()’ method.
takeuforward
takeuforward.org › data-structure › reverse-a-string
Reverse a String - Tutorial
Search for a command to run
InterviewBit
interviewbit.com › coding problems › reverse string (c++, java, and python)
Reverse String (C++, Java, and Python) - InterviewBit
June 23, 2023 - def reverseString(self, s): def helper(left, right): if left < right: s[left], s[right] = s[right], s[left] helper(left + 1, right - 1) helper(0, len(s) - 1) Time Complexity:O(N), where N is the length of the string. Space Complexity:O(N), since the recursion stack takes space. Another simple approach to solve this problem is to use two pointers method.
GeeksforGeeks
geeksforgeeks.org › java › reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: Characters are stored in a list and reversed using Collections.reverse(). This approach is helpful when you’re already working with Java collections. StringBuffer is similar to StringBuilder but thread-safe.
Published October 14, 2025
YouTube
youtube.com › watch
Reverse String - Leetcode 344 - 2 Pointers - (Python) - YouTube
Master Data Structures & Algorithms for FREE at https://AlgoMap.io/Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: h...
Published April 17, 2024
AlgoMonster
algo.monster › liteproblems › 344
344. Reverse String - In-Depth Explanation
In-depth solution and explanation for LeetCode 344. Reverse String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
Top answer 1 of 4
3
After while(*sptr1!='\0')... sptr points to the null-terminator of the string and then you are switching this null terminator with the first character. E.g. you move the null terminator to index 0. You have to decrement sptr before starting the reverse.
You should also decrement len by 2, otherwise you would iterate over the whole array and switch the already switched characters back.
Some other small mistakes:
main should return int, not void.
scanf("%s", &str1); should be scanf("%s", str1);, str1 already decays to a pointer.
You should add \n in your printf statements to have the output in different lines instead of 1 long line.
#include<stdio.h>
int main() {
char str1[10];
char temp;
char *sptr1;
char *sptr2;
int len;
printf("Enter a string:\n");
scanf("%s", str1);
sptr1 = str1;
sptr2 = str1;
while ( *sptr1 != '\0') {
sptr1++;
}
len = sptr1 - str1;
printf("Length of the string:%d\n", len);
sptr1--;
while (len > 0) {
temp = *sptr1;
*sptr1 = *sptr2;
*sptr2 = temp;
sptr1--;
sptr2++;
len = len-2;
}
printf("%s\n", str1);
}
See it live: https://ideone.com/WAnQLi
2 of 4
0
#include<stdio.h>
#include<string.h>
int main(int argc, const char * argv[])
{
char s[]="hello";
strrev(s);
puts(s);
return 0;
}
try strrev function:
char *strrev(char *str);
AlgoMonster
algo.monster › liteproblems › 541
541. Reverse String II - In-Depth Explanation
In-depth solution and explanation for LeetCode 541. Reverse String II in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.