November 22, 2024
mumbling

Belajar Bahasa C – Mumbling

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;
}

Rajo Intan

Blogger, pemiliki Onestring Lab, menulis artikel terkait teknologi informasi dan pendidikan. Web Developer, berpengalaman lebih dari 20 tahun mengembangkan berbagai aplikasi dan sistem informasi. Kerjasama kontak di onestringlab@gmail.com atau https://forms.gle/xAGKkpi6B3BzJyzk7

View all posts by Rajo Intan →

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock