Q-27: Write a C/C++ program to reverse a given string.
    

Using - C


#include<stdio.h> #include<string.h> void main() { char str[1024]; int length, i, temp; printf("Enter a string: "); gets(str); length = strlen(str); for(i=0; i < length/2; i++) { temp = str[i]; str[i] = str[length-1-i]; str[length-1-i] = temp; } printf("Reversed: %s",str); }

OUTPUT

Enter a string: Pro Player
Reversed:  reyalP orP

        

Using - C++


#include<iostream> #include<string.h> using namespace std; int main() { string str; int length, temp; cout<< "Enter a string: "; getline(cin,str); length = str.length(); for(int i=0; i < length/2; i++) { temp = str[i]; str[i] = str[length-1-i]; str[length-1-i] = temp; } cout<< "Reversed: "<< str; return 0; }

OUTPUT

Enter a string: algorithms
Reversed:  smhtirogla