Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Swapping two variables using temporary third variable.
#include
#include
using namespace std;
int main()
{
char str[80];
cout<<"Input String to Be Reversed : " ;
cin.getline(str,80);
int len = strlen(str);
char temp;
for(int i=0; i
×
Ask AI About This Code
Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.
×
Test CPP
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Input String to Be Reversed : Zero was invented by India
aidnI yb detnevni saw oreZ
Code Understanding
char str[80]; cout<<“Input String to Be Reversed : ” ; cin.getline(str,80);
String is collected in char array str.
int len = strlen(str); //The string length is calculated using the function for loop traversing char temp; //A temp variable is declared which will help to keep data while reversal swapping for(int i=0; i<len/2; i++)
The reversal loop will be traversed till mid point only as for reversal swapping of variables till mid point is sufficient.
{ temp=str[i]; str[i]=str[len-i-1]; str[len-i-1]=temp;
This is standard array reversal procedure as explained in #2506
} cout <<str<<endl; //The reversed entry is printed.
Notes
The reversal logic here uses the reversal of individual characters. This reversal situation applies to any data type, char, int, long, float, double etc. as long as they are in an array.