Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to repeat a character N times in C

3 Answers

0 votes
#include <stdio.h>

int main()
{
    int N = 5; 
    char ch = 'z';

    for (int i = 0; i < N;i++){
        printf("%c", ch);    
    }
    
    return 0;
}




/*
run:

zzzzz

*/

 



answered Feb 5, 2021 by avibootz
0 votes
#include <stdio.h>

void repeat_char(char ch , int N) {
     for (int i = 0; i < N; i++) {
        printf("%c", ch);
    }
}

int main()
{
    repeat_char('e', 4);
    
    return 0;
}




/*
run:

eeee

*/

 



answered Feb 5, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main() {
    char ch = 'a';
    int totaltimes = 5;
 
    char *p = (char *)malloc(sizeof(char) * totaltimes + 1);
 
    for (int i = 0; i < totaltimes; i++) {
         p[i] = ch;
    }
 
    puts(p);
 
    free(p);
 
    return 0;
}
 
 
 
 
/*
run:
 
aaaaa
 
*/

 



answered Feb 21, 2022 by avibootz

Related questions

1 answer 167 views
1 answer 170 views
1 answer 138 views
138 views asked Feb 1, 2022 by avibootz
1 answer 57 views
2 answers 201 views
1 answer 63 views
63 views asked Dec 23, 2024 by avibootz
3 answers 273 views
...