#include using std::string; /** * Problem 1: Simple strings & loops * * Given as input string and an int N, set result * a string made of N repetitions of the last N characters * of the string. You may assume that N is between 0 and * the length of the string, inclusive. * * - for input of "Hello", 3 -> "llollollo" * - for input of "Hello", 2 -> "lolo" * - for input of "Hello", 1 -> "o" */ string repeatN(const string& str, int n) { string result = "not done"; // ---- YOUR CODE GOES ONLY BELOW THIS LINE // ---- YOUR CODE GOES ONLY ABOVE THIS LINE return result; }
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
#include <string>
using std::string;
/**
* Problem 1: Simple strings & loops
*
* Given as input string and an int N, set result
* a string made of N repetitions of the last N characters
* of the string. You may assume that N is between 0 and
* the length of the string, inclusive.
*
* - for input of "Hello", 3 -> "llollollo"
* - for input of "Hello", 2 -> "lolo"
* - for input of "Hello", 1 -> "o"
*/
string repeatN(const string& str, int n)
{
string result = "not done";
// ---- YOUR CODE GOES ONLY BELOW THIS LINE
// ---- YOUR CODE GOES ONLY ABOVE THIS LINE
return result;
}
/**
* Problem 2: Intermediate Strings & Loops
*
* Given a string and a non-empty word string, set result
* to a version of the original string where all chars
* have been replaced by pluses ("+"), except for appearances
* of the word string which are preserved unchanged.
*
* Some examples:
* - for input of "12xy34", "xy" -> "++xy++"
* - for input of "12xy34", "1" -> "1+++++"
* - for input of "12xy34xyabcxy", "xy" -> "++xy++xy+++xy"
*/
string replacePlus(const string& str, const string& word)
{
string result = "not done";
// ---- YOUR CODE GOES ONLY BELOW THIS LINE
// ---- YOUR CODE GOES ONLY ABOVE THIS LINE
return result;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images