Create a “word machine” capable of asking the user to enter a word, and then let
ID: 3771249 • Letter: C
Question
Create a “word machine” capable of asking the user to enter a word, and then let the user perform a given task from a menu. The menu should provide the following task options: 1. Count the letters – count all the letters in the given word. 2. Count the vowels – vowels are the letters a, e, i, o, u. Your program should count how many vowels appear in the word entered by the user. 3. Reverse the word – reversing means to display the last characters in a string first and the first characters in a string last. 4. Check if palindrome – a palindrome is a word that is spelled the same forwards and backwards. Examples of palindromes are ‘mom’ and ‘kayak’
You only need to work with single words
Your program must make use of functions
C Programming
Explanation / Answer
Here is the code for you. If you have any further queries, just get back to me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int countLetters(char word[])
{
return strlen(word);
}
int countVowels(char word[])
{
int wordCount = 0;
for(int i = 0; i < strlen(word); i++)
if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u')
wordCount++;
return wordCount;
}
char *stringReverse(char word[])
{
char wordR[50];
strcpy(wordR, word);
strrev(wordR);
}
int isPalindrome(char word[])
{
char wordR[50];
strcpy(wordR, word);
stringReverse(word);
if(strcmp(word, wordR))
return 1;
return 0;
}
int main()
{
char word[50];
int choice;
printf("Please enter your word: ");
scanf("%s", word);
while(1)
{
printf("Menu Options: ");
printf("1. Count the letters of a given word. ");
printf("2. Count the vowels in a given word. ");
printf("3. Reverse the string. ");
printf("4. Check if the word is a palindrome. ");
printf("5. Exit. ");
printf("Enter your choice: ");
scanf("%i", &choice);
switch(choice)
{
case 1: printf("The number of letters in the word is: %i. ", countLetters(word)); break;
case 2: printf("The number of vowels in the word is: %i. ", countVowels(word)); break;
case 3: printf("The reverse of the word %s is %s ", word, stringReverse(word)); break;
case 4: if(isPalindrome(word))
printf("The string %s is a palindrome. ", word);
else
printf("The string %s is not a palindrome. ", word);
case 5: exit(0);
default: printf("Invalid choice. ");
}
}
If you need any further refinement, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.