Approach

Written by Jeffrey Yao from CSESoc Education

A good way to approach questions is to break down what you have to do. So, what do we have to do?

  • Read the input string
  • Iterate over the string word-wise
  • Check if it's the first vowel or second consonant.
  • If it is, convert the letter to uppercase.
  • Once we've finished iterating over the word, print it.

Let's tackle each task one-by-one.

Read the input string

We use the fgets() function to read a string in. First, we initialise a char array with a MAX_LENGTH of 1024 according to the input constraints, then read the string.

// Define MAX_LENGTH in header
#define MAX_LENGTH 1024
// Initialise then read string
char string[MAX_LENGTH];
fgets(string, MAX_LENGTH, stdin);

Iterate over the string word-wise

A hint was given to use the string.h library which contained a relevant function. In a sense, this also tested your ability to Google a solution quickly. The relevant function was strtok().

strtok() breaks an input string up into smaller strings (tokens) using a delimiter (a character that indicates the end of a word), then returns a pointer to the first token. In this case, the delimiter would be a space which separates words in a string. Tutorialspoint has great documentation on the function!

char *word = strtok(string, " ");
// Once word has been iterated over, move to next word.
word = strtok(NULL, " ");

Check if it's the first vowel or second consonant. If yes, convert to uppercase.

Let's break down the logic required here:

  • Check if the current letter is a vowel.
    • If it is, check if it's the first vowel. If it is, convert to uppercase and 'flag' the first vowel as found. Else, do nothing.
    • If it isn't, it must be a consonant. Iterate over the array until the second consonant is found, then convert it to uppercase.

For code style, we extract the vowel check to a separate function which returns a boolean.

bool check_if_vowel(char *word, int i)
{
// This is a lengthy condition, hence why we extract to a separate function.
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o'
|| word[i] == 'u')
{
return true;
}
else
{
return false;
}
}

check_if_vowel returns to a variable is_vowel. If is_vowel returns true for the first time, it must be the first vowel in the word. After converting the vowel to uppercase, we toggle a flag variable first_vowel to false.

For the consonants, we declare a counter variable consonant and increment by 1 everytime a consonant is found. When consonant == 2, we convert the second consonant to uppercase.

Once we've finished iterating over the word, print it

We iterate over the word until a space is reached. Once it is, we print the word.

printf("%s ", word);

Continue iterating over the string until the end is reached (word != NULL).

Sample Code

// Mocking SpongeBob
// COMP1511 Revision Session (T1, 2022)
// Written by Jeffrey Yao for CSESoc Education
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 1024
#define TRUE 0
bool check_if_vowel(char *word, int i);
int main(void)
{
// Read string using fgets.
char string[MAX_LENGTH];
fgets(string, MAX_LENGTH, stdin);
// Iterate over string word-wise.
// Begin with the first word.
char *word = strtok(string, " ");
while (word != NULL)
{
bool first_vowel = true;
int consonant = 0;
// Iterate over letters in word.
for (int i = 0; i < strlen(word) && strcmp(&word[i], " ") != 0; i++)
{
bool is_vowel = check_if_vowel(word, i);
if (is_vowel == true && first_vowel)
{
// Convert first vowel to uppercase.
word[i] = toupper(word[i]);
// First vowel found - switch flag.
first_vowel = false;
}
else if (is_vowel == false)
{
consonant++;
if (consonant == 2)
{
word[i] = toupper(word[i]);
}
}
}
// Print word.
printf("%s ", word);
// Move to next word in string.
word = strtok(NULL, " ");
}
return 0;
}
// Checks if given letter in word is vowel.
bool check_if_vowel(char *word, int i)
{
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o'
|| word[i] == 'u')
{
return true;
}
else
{
return false;
}
}