35.9k views
1 vote
Write a C++ code that will read a line of text convert it to all lower case and print it in the reverse order. Assume the maximum length of the text is 80 characters. Example input/output is shown below:

Input:

Hello sir

Output:

ris olleh

User LearnRPG
by
6.9k points

1 Answer

1 vote

C++ program for converting it into lower case and printing in reverse order

#include <iostream>

#include <string>

#include <cctype>

using namespace std;

//driver function

int main()

{

// Declaring two string Variables

//strin to store Input string

//low_string to store the string in Lower Case

string strin, low_string;

int i=0;

cout<<"Input string: "<<endl;

/*getline()method is used to store the characters from Input stream to strin string */

getline(cin,strin);

//size()method returns the length of the string.

int length=strin.size();

//tolower() method changes the case of a single character at a time.

// loop is used to convert the entire Input string to lowercase.

while (strin[i])

{

char c=strin[i];

low_string[i]=tolower(c);

i++;

}

//Checking the length of characters is less than 80

if(length<80)

{

cout<<"Output string: "<<endl;

//Printing the Input string in reverse order, character by character.

for(int i=length-1;i>=0;i--)

{

cout<<low_string[i];

}

cout<<endl;

}

else

{

cout<<"string Length Exceeds 80(Max character Limit)"<<endl;

}

return 0;

}

Output-

Input string:

Hello sir

Output string:

ris olleh

User Bertrandg
by
6.6k points