#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* repeat_char(char ch, int n) {
// Allocate memory for the string (+1 for null terminator)
char* result = (char*)malloc((n + 1) * sizeof(char));
if (result == NULL) {
perror("Memory allocation failed");
return NULL;
}
// Fill the string with the character
memset(result, ch, n);
// Null-terminate the string
result[n] = '\0';
return result;
}
int main() {
char ch = '*';
int n = 10;
char* repeated_string = repeat_char(ch, n);
if (repeated_string != NULL) {
printf("Repeated string: %s\n", repeated_string);
free(repeated_string); // Free allocated memory
}
return 0;
}
/*
run:
Repeated string: **********
*/