Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Write a program that uses a recursive function to print a string backwards

Write a program that uses a recursive function to print a string backwards

Computer Science

Write a program that uses a recursive function to print a string backwards. Your program must contain a recursive function that prints the string backwards. Do not use any global variables; use appropriate parameters.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

The problem:
Write a program that uses a recursive function to print a string backwards. Your program must contain a recursive function that prints the string backwards. Do not use any global variables; use appropriate parameters.

The solution:
#include <iostream>
#include <string> //used for string operations

using namespace std;

void strBack(char *s, int i){
if (i<0) return; //base case, terminates when the length of the string is 0
else { //general case
cout << *(s+i); //just print the character at length position i from starting position s
strBack(s, i-1); //recusrively call with one less character (at the right)
}

}
int main(){

char *str = "Hello World!";

strBack(str, strlen(str));

return 0;
}