# Reverse a String

Reverse a String” is a basic operation which can be applied over string. Here, we are given a string of length ‘n’ and our task is to write a program to reverse the given string without using in-built function.

Example:

```INPUT:
ABCDEFGH
OUTPUT:
HGFEDCBA```

There are multiple ways to reverse a string. Some of them are:

METHOD 1: Using Temporary String:

Copy the string into temporary string in reverse order and copy back the temporary string to original string.

METHOD 2: Swap Elements:

Swap first element of string with last element of string, second element of string with second last element and so on.

METHOD 1: Reverse a String using Temporary String

In this method, create a temporary string of same length as original string and copy all the elements of the original string to temporary string in reverse order. Finally, copy back temporary string to original string in original order.

Time Complexity: O(n), where n is length of string

#### C++ Program to reverse a string using temporary string is as follows:

```/* Program to reverse a string */
#include<bits/stdc++.h>
#define len 1000
using namespace std;
int main()
{
char str[len];
cout<<"Enter a string:\n";
cin>>str;
cout<<"Before Reversing String:\n";
cout<<str;
int length = strlen(str);
char temp[length+1];
int pos = 0;
/* Copy the original string to
temporary string in reverse order */
for(int i = length - 1; i >= 0; i--)
{
temp[pos] = str[i];
pos++;
}
temp[pos] = ' /* Program to reverse a string */ #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter a string:\n"; cin>>str; cout<<"Before Reversing String:\n"; cout<<str; int length = strlen(str); char temp[length+1]; int pos = 0; /* Copy the original string to temporary string in reverse order */ for(int i = length - 1; i >= 0; i--) { temp[pos] = str[i]; pos++; } temp[pos] = '\0'; /* Copy back the temporary string to original string in original order */ for(int i = 0; i < length; i++) { str[i] = temp[i]; } cout<<"\nAfter Reversing String:\n"; cout<<str; } ';
/* Copy back the temporary string to
original string in original order */
for(int i = 0; i < length; i++)
{
str[i] = temp[i];
}
cout<<"\nAfter Reversing String:\n";
cout<<str;

}
```
```OUTPUT:
Enter a String:
sdfdfv
Before Reversing String:
sdfdfv
After Reversing String:
vfdfds```

METHOD 2: Swap Elements

In this method, auxiliary string is not used. Here, we swap the first element of the string with last element of the string, second element of string with second last element and so on, until string is completely reversed.

Time Complexity: O(n), where n is length of string.

#### C++ Program to reverse a string is as follows:

```/* Program to reverse a string */
#include<bits/stdc++.h>
#define len 1000
using namespace std;
int main()
int main()
{
char str[len];
cout<<"Enter a String:\n";
cin>>str;
cout<<"Before Reversing String:\n";
cout<<str;
int length = strlen(str);
/* Swaping Elements */
for(int i = 0, j = length - 1; j >= i; i++,j--)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout<<"\nAfter Reversing String:\n";
cout<<str;

}

```
```OUTPUT:
Enter a String:
sdfdfv
Before Reversing String:
sdfdfv
After Reversing String:
vfdfds```

Related Posts: