문자열을 주어진 횟수만큼 연결하는 프로그램은 n의 값에 따라 문자열 연결 방법을 n번 실행합니다.
결과는 문자열이 여러 번 반복됩니다.
예시
given string: “ I love Tutorials point” n = 5
출력
I love Tutorials pointI love Tutorials pointI love Tutorials pointI love Tutorials point I love Tutorials point
출력을 보고 나면 함수가 수행할 작업이 명확해집니다.
예시
#include <iostream>
#include <string>
using namespace std;
string repeat(string s, int n) {
string s1 = s;
for (int i=1; i<n;i++)
s += s1; // Concatinating strings
return s;
}
// Driver code
int main() {
string s = "I love tutorials point";
int n = 4;
string s1 = s;
for (int i=1; i<n;i++)
s += s1;
cout << s << endl;;
return 0;
} 출력
I love tutorials pointI love tutorials pointI love tutorials pointI love tutorials point