To do: Print the string repeatedly, each time it prints the string, exclude the last letter until only 1 letter is left.
Given code:
#include <iostream>
#include <cstring>
using namespace std;
#define STR_MAX_SIZE 100
void preserveString(char*, int);
int main(void) {
char str[STR_MAX_SIZE];
cout << "Enter string: ";
fgets(str, STR_MAX_SIZE, stdin);
preserveString(str, strlen(str));
return 0;
}
void preserveString(char *str, int size) {
if(size > 0) {
for(int i = 0; i < size - 1; i++) {
cout << str[i];
}
cout << endl;
// TODO: Add the recursive case of the function here
}
}
#include <iostream>
#include <cstring>
using namespace std;
#define STR_MAX_SIZE 100
void preserveString(char*, int);
int main(void)
{
char str[STR_MAX_SIZE];
cout << "Enter string: ";
fgets(str, STR_MAX_SIZE, stdin);
preserveString(str, strlen(str));
return 0;
}
void preserveString(char *str, int size)
{
if (size > 0)
{
for (int i = 0; i < size - 1; i++)
{
cout << str[i];
}
cout << endl;
preserveString(str, size - 1);
}
}
Comments
Leave a comment