Question
c++

std::string addStar(const std::string &str); // Task: Given a string, compute recursively a new string where all the adjacent // Pre: str is a string (may be empty). // Post: a correctly starred string is returned // Examples: //し.addStar(hello) →Xh*e*W*o /1 addStar(abc)axb*c // addStar(ab )-→ a*b
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// helper method
std::string addStarhelp(const std::string &str, int idx)
{
// base call
int len = str.size();
if(idx == len-1 )
return str[len-1];


// recursive call
  
return str[idx] + "*" + addStarhelp(str,idx+1);
}

// addstar uses helper method to add start to string
std::string addStar(const std::string &str)
{
return addStarhelp(str, 0);
}

Add a comment
Know the answer?
Add Answer to:
c++ std::string addStar(const std::string &str); // Task: Given a string, compute recursively a new string where...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this program. C++ Given a string, compute recursively (no loops) a new...

    I need help with this program. C++ Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14". It should enter a string like: (xpix)--> and print out " x3.14x" (pi)--> and print out " 3.14p" (pipi)--> and print out " 3.143.14" This is what I have so far... #include<iostream> #include<string> using namespace std; string changePi(string str) { string left; if(str.length() < 2) return str; if(str.substr(0, 1).compare("pi")) return "3.14" + changePi(str.substr(2));...

  • [c++]Please help complete the function of binary tree(contains string data) in a recursive way std::string findPath...

    [c++]Please help complete the function of binary tree(contains string data) in a recursive way std::string findPath (const std::string &s) const; // EFFECTS: Returns the path from the root node to the node with the string s. The path is encoded by a string only containing 'O' and 'l'. Each character, from left to right, shows whether going left (encoded by '0') or right (encoded by '1') from a node can lead to the target node. For example, we want to...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string...

    This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string and a non-negative int n, return a larger string that is n copies of the original string. string_times('Hi', 2) – 'HiHi' string_times('Hi', 3) - 'HiHiHi' string_times('Hi', 1) – 'Hi' Solution: Go Save, Compile, Run (ctrl-enter) Show Solution def string_times (str, n): def string_times(str, n): result = "" for i in range(n): # range(n) is [0, 1, 2, .... n-1] result = result + str...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...

    In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...

    This needs to be done in c++11 and be compatible with g++ compiling Project description: Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52 cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT