Question

please help with this c++ function for my study guide Write the function repeatFront such that...

please help with this c++ function for my study guide

Write the function repeatFront such that given a string and an int n, it returns a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the size of the string, inclusive (i.e. n >= 0 and n <= str.size()). Hint: Solve this problem by using C++ strings not C-strings. Examples: repeatFront("Chocolate", 4) → "ChocChoChC" repeatFront("Chocolate", 3) → "ChoChC" repeatFront("Ice Cream", 2) → "IcI" repeatFront("Ice Cream", 0) → ""

0 0
Add a comment Improve this question Transcribed image text
Answer #1
string repeatFront(string str, int n) {
    string res=str.substr(0,n);
    for(int i=1;i<n;i++){
        res=res+str.substr(0,n-i);
    }
    return res;
}
Add a comment
Know the answer?
Add Answer to:
please help with this c++ function for my study guide Write the function repeatFront such that...
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
  • please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must chec...

    please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must check if m is within the length of both s and t. int submatch(char* s, char* t, int n, int m) Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match....

  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • Write the following program in C Language using standard library functions. /*Implement the Find function, called...

    Write the following program in C Language using standard library functions. /*Implement the Find function, called in the program below. This function receives two strings, and looks for the first occurrence of the second string in the first string, returning the number of characters it is in, relative to the beginning of the first string. If not, returns -1. In the program, a string with two news headlines is given, and the sub-strings "iPad" and "Huawei” are searched for, having...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Do it in C please, without using parseint, atoi. * The isinteger ) function examines the...

    Do it in C please, without using parseint, atoi. * The isinteger ) function examines the string given as its first argument, and returns true if and only if the string represents a well-formed integer. A well-formed integer consists only of an optional leading - followed by one or more decimal digits. Returns true if the given string represents an integer, false otherwise. bool isinteger (char +str) *The parseint ) function parses a well-formed string representation of * an integer...

  • Please use C++ This first is required function, second is my function, can you help me...

    Please use C++ This first is required function, second is my function, can you help me to fix my function using bitwise and bit shifting operators? bool all_uniqueletters (const string &s) { I TODO: returns true if al1 letters in string are unique, that is // no duplicates are found; false otherwise // You may use only a single int for storage and work with bitwise // and bitshifting operators // No credit will be given for other solutions.

  • i need help with my my cs c++ assignment 1. So write me some function declarations...

    i need help with my my cs c++ assignment 1. So write me some function declarations for these: A) A function called EatPancakes that takes a string for an argument and returns an int. Void EatPancakes() { } B) A function called DeleteThis that takes a Rock pointer and returns nothing Void DeleteThis() { } C) A function called Exchange that takes two ints and returns nothing Void Exchange() { } D) A function called GetName that takes no arguments...

  • The following are questions from a study guide, could I get some good examples of c++...

    The following are questions from a study guide, could I get some good examples of c++ code with comments for a better understanding, thanks! 2.Be prepared to implement a stack, queue, or priority queue using a linked list (singly or doubly linked). 3.Be prepared to implement a “weird” method that you’ve never heard of before to prove you truly understand what you are doing.  Here is an example: a.Implement ‘void decimate()’ which deletes every 10th item from the list: https://en.wikipedia.org/wiki/Decimation_(Roman_army) Recurison:...

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