In your source code, without much processing, probably the easiest way is with:

#define HI "hello world"
char str[] = HI " " HI " " HI;

This will declare a string of the requested value:

"hello world hello world hello world"

If you want code that will do it, you can use something like:

char *repeatStr (char *str, size_t count) {
    if (count == 0) return NULL;
    char *ret = malloc (strlen (str) * count + count);
    if (ret == NULL) return NULL;
    strcpy (ret, str);
    while (--count > 0) {
        strcat (ret, " ");
        strcat (ret, str);
    }
    return ret;
}

Now keep in mind this can be made more efficient - multiple strcat operations are ripe for optimisation to avoid processing the data over and over (a). But this should be a good enough start.

You're also responsible for freeing the memory returned by this function.


(a) Such as with:

// Like strcat but returns location of the null terminator
//   so that the next myStrCat is more efficient.

char *myStrCat (char *s, char *a) {
    while (*s != '\0') s++;
    while (*a != '\0') *s++ = *a++;
    *s = '\0';
    return s;
}

char *repeatStr (char *str, size_t count) {
    if (count == 0) return NULL;
    char *ret = malloc (strlen (str) * count + count);
    if (ret == NULL) return NULL;
    *ret = '\0';
    char *tmp = myStrCat (ret, str);
    while (--count > 0) {
        tmp = myStrCat (tmp, " ");
        tmp = myStrCat (tmp, str);
    }
    return ret;
}
Answer from paxdiablo on Stack Overflow
🌐
w3resource
w3resource.com › c-programming-exercises › string › c-string-exercise-32.php
C Program: Find the repeated character in a given string - w3resource
Write a C program to find all characters that occur more than once in a string and display their positions. Write a C program to determine the first character that repeats in a string by comparing each character with the rest.
Discussions

c - How to repeat a char using printf? - Stack Overflow
I'd like to do something like printf("?", count, char) to repeat a character count times. What is the right format-string to accomplish this? More on stackoverflow.com
🌐 stackoverflow.com
repeat char - C++ Forum
hi, i'm new at C :s is there a way to print one char for n times without using for or similar? thanks ... Probably, but why would you? The definition of "for" is pretty much "a loop for when you want to repeat something n times". More on cplusplus.com
🌐 cplusplus.com
repeat in a string - C++ Forum
So I am writing a code where i want to print out the amount of letters in a string and print it out once but have minor problems of reading the previous letter. More on cplusplus.com
🌐 cplusplus.com
Is there a way to repeat characters in a string in C? - Stack Overflow
I'm making a Vigenère cipher implementation in Arduino; and I wanted to build the functions in C first, just to get the hang of things. My key strings will be stored in the arduino, and they will be More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnprogramming › string repetition in c
r/learnprogramming on Reddit: string repetition in C
March 11, 2018 -

I'm taking the cs50 course on edX and I'm at the second problem. One of the things to do is to print a sequence of characters.

Now, in Python i would simply do:

print('#' * 5)

and get:

#####

What's the equivalent in C? Do I have to use a for loop?

🌐
YouTube
youtube.com › portfolio courses
Repeat A String | C Programming Example - YouTube
How to repeat a string using C. For example, repeating the string "abc" 3 times would result in the string "abcabcabc". Source code: https://github.com/port...
Published   January 31, 2024
Views   2K
🌐
Rosetta Code
rosettacode.org › wiki › Repeat_a_string
Repeat a string - Rosetta Code
3 weeks ago - ora b mov m,c ; then empty string rz rpt1: push d ; save begin of string to repeat chcpy: ldax d ; copy character from input to output mov m,a inx d ; advance pointers inx h cmp c ; end of string? jnz chcpy pop d ; restore begin of string to repeat dcx h ; move back past terminator in copy dcr b ; done yet?
🌐
Reactgo
reactgo.com › home › how to repeat a character n times in c
How to repeat a character n times in C | Reactgo
August 16, 2023 - #include <stdio.h> void repeat (char c , int count ) { for (int i = 0; i<count;i++){ printf("%c", c); } } int main() { repeat('a', 3); return 0; } Share: How to get the last character of a string in CHow to print hello world in CHow to find the length of a given string in CHow to get the first n characters of a string in CHow to convert the String to Integer in CHow to get the first character of a string in C ·
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 139884-[problem]-string-repeat-string-s-int-n-creates-returns-new-string.html
[Problem] String repeat(String s, int n) that creates and returns a new string
Hi eveyone Question is :- String repeat(String s, int n) that creates and returns a new string that is made by concatenating n copies of the parameter string s. For example, calling this method with the parameters “Hello” and 3 would return the string “HelloHelloHello”. If n equals ...
Find elsewhere
🌐
Evan Hahn
evanhahn.com › short-c-program-to-repeat-a-string-to-stdout
Short C program that repeats a string forever - Evan Hahn
September 1, 2023 - Before Sven reached out, I couldn’t find a good way to do this, so I gave up and wrote a short C program to do it. Here’s it is: #include <stdio.h> #include <string.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "error: please supply exactly one argument\n"); return 1; } char *arg = argv[1]; if (strlen(arg) == 0) { fprintf(stderr, "error: please no empty strings\n"); return 1; } while (fputs(arg, stdout) != EOF) { } return 0; }
🌐
Sololearn
sololearn.com › en › Discuss › 1556184 › repeat-a-string-n-times-with-the-character-c-between
Repeat a string n times with the character c between
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Sololearn
sololearn.com › en › Discuss › 478032 › repeat-string-in-c
Repeat string in c++ | Sololearn: Learn to code for FREE!
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Quora
quora.com › How-do-you-write-a-C-program-for-checking-a-string-for-repeated-characters-traversing-it-only-once
How to write a C program for checking a string for repeated characters, traversing it only once - Quora
Answer (1 of 2): Assuming you're allowed to use some extra storage, keep an array of all characters (CHAR_MAX + 1) and initialize it to 0. While traversing the string set the entry in array for a character to 1 on first occurrence of that character. If the entry for that character in the array is...
🌐
Sololearn
sololearn.com › en › Discuss › 3272615 › repeating-a-string
Repeating a string | Sololearn: Learn to code for FREE!
April 10, 2024 - In C++, you can repeat a string `n` times using various methods. One simple way is to use a loop to concatenate the string `n` times.
🌐
Cplusplus
cplusplus.com › forum › beginner › 55961
repeat char - C++ Forum
hi, i'm new at C :s is there a way to print one char for n times without using for or similar? thanks ... Probably, but why would you? The definition of "for" is pretty much "a loop for when you want to repeat something n times".
🌐
Tutorjoes
tutorjoes.in › c_programming_tutorial › print_value_using_for_loop_in_c
Printing a String Multiple Times in C Programming
This program is written in the C programming language, and it is used to print the string · "Hai" a specified number of times. The program starts by including the standard input/output header file, ... scanf() function. The for loop is then used to repeatedly execute the code inside the loop for the number of times specified by the user.
🌐
Cplusplus
cplusplus.com › forum › beginner › 272259
repeat in a string - C++ Forum
favor [] over .at(); its faster, only use .at() if you may go out of bounds (it checks). there are 2 fairly straightforward ways to do this. 1) sort a copy of the string, and iterate, it will be in a form abbbcddde etc where you can just count the same ones until it changes to something else.