# Reverse a String

Difficulty Level Easy
Frequently asked in Infosys MAQ o9 solutions TCS
StringViews 1250

## Problem Statement

“Reverse a String” problem states that you are given a string s of size n. Write a program to reverse it. So, what does reversing a string means? It generally means reversing the input string that we are given. That is it is defined as an operation doing which leads to change in the position of original characters in a word. The character which is present at the start of the string goes to end. Similarly, other characters go to different indices in the same fashion ( more formally ith character goes to length of string -i -1 position).

### Example `TutorialCup`
`puClairotuT`
`Akshita`
`atihskA`

## Swapping Method

### Algorithm

```1. Initialise a string s of length n.
2. Create a function to reverse a string that accepts a string variable as it's a parameter.
3. Traverse through the string and swap the starting letter with ending letter until mid is reached.
4. Return the reversed string variable.```

### Complexity Analysis

#### Time complexity

O(n) where n is the number of characters in the given word. Since we only traversed the input.

#### Space Complexity

O(1) because we used constant extra space. We did not create any temporary string or array.

### Code to reverse a string

#### C++ Program

```#include <bits/stdc++.h>
using namespace std;

string reverse(string s){
int l = s.length();

for(int i=0; i<l/2; i++)
swap(s[i], s[l-i-1]);

return s;
}

int main(){
string s = "TutorialCup";
cout<<reverse(s);
return 0;
}```
`puClairotuT`

#### Java Program

```class reverseString{

static String reverse(String input)
{
StringBuilder str = new StringBuilder(input);
int length = str.length();
for (int i = 0; i < length / 2; i++)
{
char current = str.charAt(i);
int otherEnd = length - i - 1;
str.setCharAt(i, str.charAt(otherEnd)); // swap
str.setCharAt(otherEnd, current);

}
return str.toString();
}
public static void main (String[] args){
String s = "TutorialCup";
System.out.println(reverse(s));
}
}```
`puClairotuT`

## Using Inbuilt Reverse Method

### Algorithm

```1. Initialize a string s of length n.
2. Use the inbuilt reverse function on it.
3. Print the reversed string variable.```

### Complexity Analysis

#### Time complexity

O(n) where n is the number of characters in the given word.

#### Space Complexity

O(1) because we used constant extra space.

### Code to reverse a string

#### C++ Program

```#include <bits/stdc++.h>
using namespace std;

int main(){
string s = "TutorialCup";

reverse(s.begin(), s.end());

cout<<s;
return 0;
}```
`puClairotuT`

#### Java Program

```import java.lang.*;
import java.io.*;
import java.util.*;

class reverseString{

public static void main(String[] args){
String s = "TutorialCup";

StringBuilder sobj = new StringBuilder();

sobj.append(s);

sobj = sobj.reverse();

System.out.println(sobj);
}
}```
`puClairotuT`

## Printing Reverse Only

### Algorithm to reverse a string

```1. Initialize a string s of length n.
2. Create a function to reverse a string that accepts a string variable as it's a parameter.
3. Traverse through the given string variable from the last character to the first character.
4. Print every character of the string variable given.```

### Complexity Analysis

#### Time complexity

O(n) where n is the number of characters in the given word.

#### Space Complexity

O(1) because we used constant extra space.

### Code to reverse a string

#### C++ Program

```#include <bits/stdc++.h>
using namespace std;

void reverse(string s){
for(int i=s.length()-1; i>=0; i--)
cout<<s[i];
}

int main(){
string s = "TutorialCup";
reverse(s);
return 0;
}```
`puClairotuT`

#### Java Program

```import java.lang.*;
import java.io.*;
import java.util.*;

class reverseString{

public static void main(String[] args){

String s = "TutorialCup";

char[] s1 = s.toCharArray();

for(int i=s1.length-1; i>=0; i--)
System.out.print(s1[i]);
}
}```
`puClairotuT`

## Using Character Array

### Algorithm to reverse a string

```1. Initialize a string s of length n.
2. Create a function to reverse a string that accepts a string variable as it's a parameter.
3. Create a character array and copy the given string variable in it.
4. Traverse through the character array till the mid swapping the starting characters and the ending characters.
5. Return the character array containing the reversed string.```

### Complexity Analysis

#### Time complexity

O(n) where n is the number of characters in the given word.

#### Space Complexity

O(n) because we used n extra space.

### Code to reverse a string

#### C++ Program

```#include <bits/stdc++.h>
using namespace std;

char* reverse(char const* s){
int l = strlen(s);

char *rev = new char[l+1];

strcpy(rev, s);

for(int i=0, j=l-1; i<j; i++,j--)
swap(rev[i], rev[j]);

return rev;
}

int main(void){
const char *s = "TutorialCup";
cout<<reverse(s);
return (0);
}
```
`puClairotuT`

#### Java Program

```import java.lang.*;
import java.io.*;
import java.util.*;

class reverseString{

public static void main(String[] args){
String s = "TutorialCup";
char[] temp = s.toCharArray();
int left, right=0;
right = temp.length-1;

for(left=0; left<right ; left++ ,right--){
char t = temp[left];
temp[left] = temp[right];
temp[right]=t;
}

for (char c : temp)
System.out.print(c);
System.out.println();
}
}```
`puClairotuT`

Translate »