Reverse a given String

One of the most popular interview question for both freshers as well as experience is this, Pass a string as a parameter. Reverse the string and return it. All the alphabets in the strings should be individually reversed and then returned.



Test Case – 1
Input: B2Tech
Output: hceT2B
Test Case – 2
Input: BushanSirgur.in
Output: ni.rugriSnahsuB



JavaC++CPython
[java] /* Java program to reverse a given String without using reverse() */

public class StringReverse {
public static void main(String[] args) {
System.out.println(reverseString(“B2Tech”));
System.out.println(reverseString(“BushanSirgur.in”));
}
public static String reverseString(String string){
char c[] = string.toCharArray();
int last = c.length – 1;
for(int first = 0; first < last; first++, last–){
char temp = c[first];
c[first] = c[last];
c[last] = temp;
}
String reversedString = new String(c);
return reversedString;
}
}
[/java]

[cpp] #include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << “Enter a string : “;
gets(str);
j = strlen(str) – 1;
for (i = 0; i < j; i++,j–)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << “\nReverse string : ” << str;
return 0;
}
[/cpp]
There are so many ways to reverse a string, we are looking at the top 2 ways to reverse a string

Approach #1
In this approach, it allows the user to enter any string or character array. Next, it will use For Loop to iterate each and every character from the last character, and save the characters in reverse order.

[c] /* C program to Reverse a String without using strrev() */

#include <stdio.h>
#include <string.h>

int main()
{
char Str[100], RevStr[100];
int i, j, len;

printf(“\n Please Enter any String : “);
gets(Str);

j = 0;
len = strlen(Str);

for (i = len – 1; i >= 0; i–)
{
RevStr[j++] = Str[i];
}
RevStr[i] = ‘\0’;

printf(“\n String after Reversing = %s”, RevStr);

return 0;
}
[/c]

Approach #2

In this approach, we are using the temp variable to change the location of the character. It is something like swapping technique.

[c] /* C program to Reverse a String without using strrev() */

#include <stdio.h>
#include <string.h>

int main()
{
char Str[100], temp;
int i, j, len;

printf(“\n Please Enter any String : “);
gets(Str);

len = strlen(Str) – 1;

for (i = 0; i < strlen(Str)/2; i++)
{
temp = Str[i];
Str[i] = Str[len];
Str[len–] = temp;
}

printf(“\n String after Reversing = %s”, Str);

return 0;
}
[/c]

There are so many ways to reverse a string, we are looking at the top 2 ways to reverse a string

Approach #1 Using for loop

[python] def reverse(s):
str = “”
for i in s:
str = i + str
return str

s = “B2Tech”

print (“The original string is : “,end=””)
print (s)

print (“The reversed string(using loops) is : “,end=””)
print (reverse(s))
[/python]

Approach #2 Using extended Slice syntax

[python] # Function to reverse a string
def reverse(string):
string = string[::-1] return string

s = “bushansirgur.in”

print (“The original string is : “,end=””)
print (s)

print (“The reversed string(using extended slice syntax) is : “,end=””)
print (reverse(s))
[/python]

That’s it for this article, I hope this article is helped you in one or the other way. If you have any questions ask me in the comment section, I will try to answer your question. My name is Bushan, I will see you in the next article.



 

About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *