#include #include #include // When calling this funcion, please be aware that input must be twice as big as the string it is storing // Like this: ['b', 'e', 'd', 0, '', '', ''] // If not your program will crash or maybe do something weird void double_letters(char *input){ // An in-place solution. Will it be slow? int final_length = strlen(input) * 2; for(int i = 0; i < final_length; i+=2){ // shuffle everything over for(int j = strlen(input); j > i; j--) input[j] = input[j-1]; input[i+1] = input[i]; } } char* allocate_and_double_letters(char *input){ char *return_location = malloc(strlen(input) * 2); strcpy(return_location, input); double_letters(return_location); return return_location; } int main(){ char bug[19] = "cockroach"; double_letters(bug); printf("bug = %s\n", bug); char *another_bug = "bedbug"; char *tmp = allocate_and_double_letters(another_bug); printf("another_bug modifed is: %s\n", tmp); free(tmp); return 0; }