/* * File: ncopies.c * --------------- * building a string consisting of n copies of a given string */ #include #include #define MaxStrIn 20 #define MaxStrOut 200 main() { char strIn[MaxStrIn], strOut[MaxStrOut], space_ans[4]; int i, n, space_opt; printf("Input string: "); scanf("%s", strIn); printf("How many copies? "); scanf("%d", &n); printf("Space in between [yes/no]? "); scanf("%s", space_ans); if (!strcmp(space_ans, "yes") || !strcmp(space_ans, "Yes")) { space_opt = 1; } else { space_opt = 0; } if ( n * strlen(strIn) >= MaxStrOut ) { printf("Warning: Output string too long. Exit."); exit(-1); } strOut[0] = '\0'; for (i = 0; i < n; i++) { strcat(strOut, strIn); if (space_opt) strcat(strOut, " "); } printf("Output string below:\n%s\n", strOut); }