Advertisements
Advertisements
Question
Write a C++ program to accept a sentence (maximum 50 characters) and print the sentence in reverse.
Answer in Brief
Solution
//Reverse a string
#include
void main ( )
{
char str [50], reverse [50];
int len, i, k;
cout << “Enter a string :” <<endl;
cin>>str;
for (i = 0; str [i]! = ‘\0’ ; i++)
len = i;
len - -;
for (k = 0; len >=0; len - -, k++)
reverse [k] = str [len];
reverse [k] = ‘\0’;
cout <<“reverse string is :”;
cout <<reverse;
}
shaalaa.com
C++ Programming
Is there an error in this question or solution?