October 31, 2024
Mexican Wave

Belajar Bahasa C – Mexican Wave

Introduction

The wave (known as the Mexican wave in the English-speaking world outside North America) is an example of metachronal rhythm achieved in a packed stadium when successive groups of spectators briefly stand, yell, and raise their arms. Immediately upon stretching to full height, the spectator returns to the usual seated position.

The result is a wave of standing spectators that travels through the crowd, even though individual spectators never move away from their seats. In many large arenas the crowd is seated in a contiguous circuit all the way around the sport field, and so the wave is able to travel continuously around the arena; in discontinuous seating arrangements, the wave can instead reflect back and forth through the crowd. When the gap in seating is narrow, the wave can sometimes pass through it. Usually only one wave crest will be present at any given time in an arena, although simultaneous, counter-rotating waves have been produced. (Source Wikipedia)

Task

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up. The problem link is here.

Rules

  • The input string will always be lower case but maybe empty.
  • If the character in the string is whitespace then pass over it as if it was an empty seat

Example

wave("hello") => {"Hello", "hEllo", "heLlo", "helLo", "hellO"}

Solution

void wave(const char *y, char **target)
{
  /* you don't have to allocate memory, use target */
  /* y string is always null terminated */
  /* each string in target has length strlen(y) + 1 */

  int i, k;
  char temp;
  int slen = strlen(y);
  char *c = (char *)calloc((slen + 1), sizeof(char));

  for (i = 0; i < slen; i++)
  {
    strcpy(c, "");
    for (k = 0; k < slen; k++)
    {
      temp = (k == i && y[k] != 32) ? toupper(y[k]) : y[k];
      strncat(c, &temp, 1);
    }
    if (y[i] != 32)
      strcpy(*target++, c);
  }
}

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