Answer to Question #330701 in C++ for Secret Agent

Question #330701

 I Don't Feel So Good

by CodeChum Admin

In order to preserve Spiderman's last words, Mr. Stark wants us to create a program that takes a string and output the string with each proceeding output having one less character until it outputs only one string.


Instructions:

  1. In the code editor, you are provided with a main() function that asks the user for a string and passes this string and the size of this string to a function call of the function, preserveString().
  2. This preserveString() function has the following description:
  3. Return type - void
  4. Name - preserveString
  5. Parameters
  6. The string
  7. Length of the string
  8. Description - this is a recursive function that prints the string repeatedly. Each time it prints the string, it excludes the last character of the string until only one character is left.
  9. This preserveString() function has already been partially implemented. Your only task is to add the recursive case of this function.

Input


1. String to be processed

Output


Enter·string:·Hello!
Hello!
Hello
Hell
Hel
He
H
1
Expert's answer
2022-04-19T08:07:05-0400
#include <iostream>
#include <cstring>
using namespace std;


void preserveString(char str[], int n) {
    if (n == 0) {
        return;
    }


    for (int i=0; i<n; i++) {
        cout << str[i];
    }
    cout << endl;
    preserveString(str, n-1);
}


int main() {
    char str[100];

    cout << "Enter string: ";
    cin >> str;

    preserveString(str, strlen(str));

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS