Signup/Sign In

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

  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


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.