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 problem link is here.
Solution:
char *accum(const char *source)
{
char temp;
unsigned long i, j, mem;
unsigned long length = strlen(source);
mem = strlen(source);
for (i = 0; i <= length; i++)
{
mem += i;
}
char *mumbling = calloc(mem + 1, sizeof(char));
for (i = 0; i < length; i++)
{
for (j = 0; j < i + 1; j++)
{
temp = (j == 0) ? toupper(source[i]) : tolower(source[i]);
strncat(mumbling, &temp, 1);
}
(i < length - 1) ? strncat(mumbling, "-", 1) : "";
}
return mumbling;
}