Q11. Write a code to reverse a string
Given a string and the task is to write a algorithm and program to reverse a string. A string is sequence of characters terminated with a null character \0. So we are given a input string and we need to print its reverse order.
For Example: We have given input string as “ALFATECHLAB” and it’s reverse output we have to find out.
Input=”ALFATECHLAB”
Output=”BALHCETAFLA”
Explanation:
1. First declare a string variable and initialize an empty string to “ALFATECHLAB”.
2. Now we have to calculate the length of the string which we can do using loop(increment the value of count until the string is \0) or using the in-build function(strlen function).
3. Define two variables start = 0 and end = length -1.(Suppose “ALFATECHLAB” has a length of 11, then we will swap the 0th character with the 10th character, the 1st character with the 9th character, and the loop will iterate until length / 2 = 5).
4. Iterate the loop from i = 0 to i = length / 2 and increment the variable i by 1.
5. As we already define the start and end variable values, so now we just have to swap the characters one by one and increment the value of start += 1 and decrement the value of end- = 1 during each iteration.
6. After completing iteration print the string and output will be “BALHCETAFLA”.
Algorithm For Reverse A String
//Algorithm To Reverse A String
START
Step 1 -> Input string str1.
Step 2 -> calculate the length of the string.
Step 3 -> define start=0 and end=length-1;
Step 4 -> for i=0 to length/2.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1;
Loop End
step 5-> Display String str1.
STOP
Code For Reverse A String
//C Program To Reverse A String.
#include <stdio.h>
int main()
{
char str1[100]=”coding in piro”;
int count = 0,i;
//Count the length using While loop
while (str1[count] != ‘\0’)
count++;
int start=0,end=count-1;
char temp;
for(int i=0;i<count/2;i++)
{
//temp is used as a temporary variable that swaps two characters.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1;
}
//print the reversed string.
printf(“%s\n”, str1);
return 0;
}
//C++ Program Reverse A String.
#include <iostream>
using namespace std;
int main()
{
string str1=”coding in piro”;
//Count the length using in-built function string.length()
int len1=str1.length(),start=0,end=len1-1;
char temp;
for(int i=0;i<len1/2;i++)
{
//temp is used as a temporary variable that swaps two characters.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1;
}
//print the reversed string.
cout<<str1;
return 0;
}
//Java Program To Reverse A String
import java.lang.*;
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
String str1 = “coding in piro”;
char[] str2 = str1.toCharArray();
int start, end=0;
//Count the length using in-built function string.length.
end = str2.length-1;
for (start=0; start < end ; start++ ,end–)
{
//temp is used as a temporary variable that swaps two characters.
char temp = str2[start];
str2[start] = str2[end];
str2[end]=temp;
}
//print the reversed string.
for (char c : str2)
System.out.print(c);
System.out.println();
}
}
//Python Program To Reverse A String.
def reverse(str1):
str2 = “”
for i in str1:
\\define a empty string str2 and insert the character from starting.
str2 = i + str2
return str2
str1 = “coding in piro”
//print the reversed string.
print (reverse(str1))
//PHP Program To Reverse A String.
<?php
function reverse($str){
//Count the length using in-built function strlen(string)
for($i=strlen($str)-1, $j=0; $j<$i; $i–, $j++)
{ //temp is used as a temporary variable that swaps two characters.
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
return $str;
}
$str = “coding in piro”;
//print the reversed string.
print_r(reverse($str));
?>
Output
orip ni gnidoc
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number