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 OverflowIn 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;
}
You could use sprintf.
char s[20] = "Hello";
char s2[20];
sprintf(s2,"%s%s%s",s,s,s);
c - How to repeat a char using printf? - Stack Overflow
repeat char - C++ Forum
repeat in a string - C++ Forum
Is there a way to repeat characters in a string in C? - Stack Overflow
Videos
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?
You can use the following technique:
printf("%.*s", 5, "=================");
This will print "====="
It works for me on Visual Studio, no reason it shouldn't work on all C compilers.
[Edit]
Note that this format specifier will print a left substring of the input, so the number you use has to be <= the width of the string
Short answer - yes, long answer: not how you want it.
You can use the %* form of printf, which accepts a variable width. And, if you use '0' as your value to print, combined with the right-aligned text that's zero padded on the left..
printf("%0*d\n", 20, 0);
produces:
00000000000000000000
With my tongue firmly planted in my cheek, I offer up this little horror-show snippet of code.
Some times you just gotta do things badly to remember why you try so hard the rest of the time.
#include <stdio.h>
int width = 20;
char buf[4096];
void subst(char *s, char from, char to) {
while (*s == from)
*s++ = to;
}
int main() {
sprintf(buf, "%0*d", width, 0);
subst(buf, '0', '-');
printf("%s\n", buf);
return 0;
}
In C, char is an integer type; if you multiply a character by an integer, you're really multiplying two integer values. But the result may not be what you expect! For instance, try running this code:
int x = 'a' * 2;
char y = 'a' * 2;
printf("x: %d\n", x);
printf("y: %d\n", y);
On ideone.com, the output is:
x: 194
y: -62
Not only that, a string in C is represented by a character array, and there is no built-in support for multiplying arrays; you have to do it yourself with a loop. So even if you have the string type provided in the CS50 library, you're out of luck.
Python isn't the only language that has this functionality, but it's a feature that you should only expect to see in higher-level languages than C, particularly those referred to as "scripting" languages.
The default implementation of Python is actually built on C code, and your question piqued my curiosity, so I asked a question on StackOverflow to find out exactly how the Python feature is implemented in C. This is definitely more advanced, and not the way you would necessarily repeat a string in pure C code, but if you're interested: How is string multiplication implemented in CPython?
You can do this:
printf("%0*i", 20, 0);
This will print 0 20 times, as shown:
00000000000000000000
However, the 'long way', for loops, is probably the way to go, as the way I showed above is a bit tedious, and complicated even for the later parts of the C section of the course.
TheBrainyOne out!