This time no story, no theory. The examples below show you how to write function accum:
Examples:
accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
The parameter of accum is a string which includes only letters from a..z and A..Z.
#include <stdlib.h>
#include <string.h>
char *accum(const char *source) {
char *ret = malloc(strlen(source) * (strlen(source) + 1));
char *str_lowercase = malloc(strlen(source) + 1);
int i = 0;
for (i = 0; source[i]; i++) {
if (source[i] >= 'A' && source[i] <= 'Z')
str_lowercase[i] = source[i] + 32;
else
str_lowercase[i] = source[i];
}
str_lowercase[i] = 0;
int x = 0;
ret[x++] = str_lowercase[0] - 32;
for (i = 1; str_lowercase[i]; i++) {
ret[x++] = '-';
ret[x++] = str_lowercase[i] - 32;
for (int j = i; j > 0; j--) ret[x++] = str_lowercase[i];
}
ret[x] = 0;
return ret;
}
너무 더럽다... 다음엔 좀 더 똑똑하게 문제 풀기로 약속.... 42식 사고방식에서 벗어나기로 약속...🤙