How to resolve the algorithm The Name Game step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm The Name Game step by step in the C programming language

Table of Contents

Problem Statement

Write a program that accepts a name as input and outputs the lyrics to the Shirley Ellis song "The Name Game".

The regular verse Unless your name begins with a vowel (A, E, I, O, U), 'B', 'F' or 'M' you don't have to care about special rules. The verse for the name 'Gary' would be like this: At the end of every line, the name gets repeated without the first letter: Gary becomes ary If we take (X) as the full name (Gary) and (Y) as the name without the first letter (ary) the verse would look like this: Vowel as first letter of the name If you have a vowel as the first letter of your name (e.g. Earl) you do not truncate the name. The verse looks like this: 'B', 'F' or 'M' as first letter of the name In case of a 'B', an 'F' or an 'M' (e.g. Billy, Felix, Mary) there is a special rule. The line which would 'rebuild' the name (e.g. bo-billy) is sung without the first letter of the name. The verse for the name Billy looks like this: For the name 'Felix', this would be right:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm The Name Game step by step in the C programming language

This C code prints a version of the "banana-fana" song for a list of names. Here's a detailed explanation:

  • Header Files:

    • The code includes two header files:
      • <stdio.h> for input/output functions like printf.
      • <string.h> for string manipulation functions like strdup and toupper.
  • print_verse Function:

    • This function takes a string name as input and prints a personalized version of the "banana-fana" song for that name.
    • It performs the following steps:
      • Creates two character pointers, x and y, to manipulate the name.
      • Initializes three integer flags b, f, and m to 1. These will be used to print "bo-", "fo-", or "mo-" based on the first letter of the name.
      • Duplicates the input name into x using strdup and converts the first letter to uppercase. The rest of the letters are converted to lowercase.
      • If the first letter of the name is a vowel ('A', 'E', 'I', 'O', or 'U'), it creates another copy (y) with the first letter in lowercase. Otherwise, y points to the second character in x.
      • Adjusts the three flags b, f, and m to 0 if the first letter of the name is 'B', 'F', or 'M' (case-insensitive).
      • Prints the song lyrics, replacing "name" with the modified x and y strings, and adjusting the prefixes "bo-", "fo-", and "mo-" based on the flag settings.
  • main Function:

    • Initializes an array names with six names.
    • Iterates through the names array, calling the print_verse function for each name to print the personalized song lyrics.
  • Code Execution:

    • When the program runs, it prints the "banana-fana" song for each name in the names array. The output will look something like this:
GARY, gary, bo-ary
Banana-fana fo-ary
Fee-fi-mo-ary
GARY!

EARL, earl, bo-earl
Banana-fana fo-earl
Fee-fi-mo-earl
EARL!

BILLY, billy, bo-illy
Banana-fana fo-illy
Fee-fi-mo-illy
BILLY!

FELIX, felix, bo-elix
Banana-fana fo-elix
Fee-fi-mo-elix
FELIX!

MARY, mary, bo-ary
Banana-fana fo-ary
Fee-fi-mo-ary
MARY!

SHIRLEY, shirley, bo-hirley
Banana-fana fo-hirley
Fee-fi-mo-hirley
SHIRLEY!

Source code in the c programming language

#include <stdio.h>
#include <string.h>

void print_verse(const char *name) {
    char *x, *y; 
    int b = 1, f = 1, m = 1, i = 1;

    /* ensure name is in title-case */
    x = strdup(name);     
    x[0] = toupper(x[0]);
    for (; x[i]; ++i) x[i] = tolower(x[i]);
   
    if (strchr("AEIOU", x[0])) {
        y = strdup(x);
        y[0] = tolower(y[0]); 
    }
    else {
        y = x + 1;
    }

    switch(x[0]) {
        case 'B': b = 0; break;
        case 'F': f = 0; break;
        case 'M': m = 0; break;
        default : break;
    }
      
    printf("%s, %s, bo-%s%s\n", x, x, (b) ? "b" : "", y);
    printf("Banana-fana fo-%s%s\n", (f) ? "f" : "", y);
    printf("Fee-fi-mo-%s%s\n", (m) ? "m" : "", y);
    printf("%s!\n\n", x);
}

int main() {
    int i;
    const char *names[6] = {"gARY", "Earl", "Billy", "Felix", "Mary", "sHIRley"};
    for (i = 0; i < 6; ++i) print_verse(names[i]);
    return 0;
}


  

You may also check:How to resolve the algorithm Egyptian division step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Color of a screen pixel step by step in the BASIC programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the C programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Bait programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Nim programming language