Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
PUBLISHED ON: APRIL 4, 2022

C++ Program To Print Reverse A Sentence

Here, we will learn how to reverse any sentence in the C++ program.

Print Reverse A Sentence in C++ Language

This program takes a sentence from the user and reverses that sentence using recursion. This program does not use strings to reverse the sentence or store the sentence.

#include <iostream>
using namespace std;
// function prototype
void reverse(const string& a);
int main() {
 string str;
 cout << " Please enter a string " << endl;
 getline(cin, str);
 
 // function call
 reverse(str);
 return 0; 
}
// function definition
void reverse(const string& str) {
 // store the size of the string
 size_t numOfChars = str.size();
 if(numOfChars == 1) {
 cout << str << endl;
 }
 else {
 cout << str[numOfChars - 1];
 // function recursion
 reverse(str.substr(0, numOfChars - 1));
 }
}


Please enter a string
Studytonight
thginotydutS

Conclusion

Here, we have learned how to reverse any sentence in the C++ program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /