fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. /**
  5.  * Function: isLegal
  6.  * -----------------
  7.  * Determines if a given word is "legal" based on the presence of vowels.
  8.  * For this problem, the letter 'Y' is considered a vowel.
  9.  *
  10.  * Rules:
  11.  * - A word is illegal if it contains no vowels (a, e, i, o, u, y).
  12.  * - Case does not matter (uppercase and lowercase are treated the same).
  13.  * - The input string is assumed to contain only letters and is null terminated.
  14.  *
  15.  * Parameters:
  16.  * inputWord[] - a null-terminated string consisting of letters only
  17.  *
  18.  * Returns:
  19.  * 1 if the word is legal (contains at least one vowel),
  20.  * 0 if the word is illegal (contains no vowels).
  21.  */
  22. int isLegal(char inputWord[]) {
  23. // Local variable to track current index in the string
  24. int stringIndex = 0;
  25.  
  26. // Local variable to hold the current character being checked
  27. char currentCharacter;
  28.  
  29. // Loop through each character in the string until null terminator is reached
  30. while (inputWord[stringIndex] != '\0') {
  31. // Assign the current character
  32. currentCharacter = inputWord[stringIndex];
  33.  
  34. // Convert to lowercase for uniform comparison
  35. currentCharacter = tolower(currentCharacter);
  36.  
  37. // Check if the current character is a vowel (including 'y')
  38. if (currentCharacter == 'a' ||
  39. currentCharacter == 'e' ||
  40. currentCharacter == 'i' ||
  41. currentCharacter == 'o' ||
  42. currentCharacter == 'u' ||
  43. currentCharacter == 'y') {
  44.  
  45. // Found a vowel → word is legal
  46. return 1;
  47. }
  48.  
  49. // Move to the next character in the string
  50. stringIndex = stringIndex + 1;
  51. }
  52.  
  53. // If loop completes without finding a vowel → word is illegal
  54. return 0;
  55. }
  56.  
  57. /**
  58.  * Function: main
  59.  * --------------
  60.  * Entry point of the program. Tests the isLegal function
  61.  * with several example words.
  62.  */
  63. int main(void) {
  64. // Test word 1: "sch" → illegal
  65. char testWord1[] = "sch";
  66. printf("%s -> %s\n", testWord1, isLegal(testWord1) ? "legal" : "illegal");
  67.  
  68. // Test word 2: "apple" → legal
  69. char testWord2[] = "apple";
  70. printf("%s -> %s\n", testWord2, isLegal(testWord2) ? "legal" : "illegal");
  71.  
  72. // Test word 3: "APPle" → legal (case insensitive)
  73. char testWord3[] = "APPle";
  74. printf("%s -> %s\n", testWord3, isLegal(testWord3) ? "legal" : "illegal");
  75.  
  76. // Test word 4: "try" → legal (contains 'y')
  77. char testWord4[] = "try";
  78. printf("%s -> %s\n", testWord4, isLegal(testWord4) ? "legal" : "illegal");
  79.  
  80. return 0; // indicate successful program end
  81. }
  82.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
sch -> illegal
apple -> legal
APPle -> legal
try -> legal