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 195 views
1 answer 200 views
1 answer 186 views
186 views asked Feb 1, 2022 by avibootz
1 answer 82 views
2 answers 224 views
1 answer 80 views
80 views asked Dec 23, 2024 by avibootz
3 answers 308 views
...