Write and test a function that takes nouns and return their plurals on the basis
ID: 3790979 • Letter: W
Question
Write and test a function that takes nouns and return their plurals on the basis of the following rules
1. if noun ends in “y” remove the “y” and add “ies”
2. if noun ends in “s” or “sh” add “es”
3. all other cases just add “s”
Print each noun and its plural. Try the following nouns.
chair diary boss circus fly dog clue dish
Structure of the program:
char* change_to_plural ( char* input_string ); // Implement this function
void main() {
//Declare required variables
//Read string
//Convert word read to represent plural by calling function
//Print the plural word
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#define MAX_WORD 50
/* prototypes */
void pluralize (char word[]);
int main (void) {
/* declarations */
char temp_word[MAX_WORD]; /* stores temporary word entered by user */
/* prompt user to enter noun */
printf("This program prints the plural form of a noun. ");
printf("Please type in a noun in singular form: ");
scanf("%s", temp_word);
while (strcmp(temp_word, "done") != 0) {
/* convert noun into plural form */
pluralize (temp_word);
/* print the plural form of the word */
printf("The plural form is %s ", temp_word);
/* prompt user to enter noun */
printf("Please type in a noun in singular form: ");
scanf("%s", temp_word);
}
/* return successfully */
return 0;
}
/* pluralize
* parameters: char word[]
* return values: none, but word[] will be changed through this function
* functionality: changes word[] to be the plural form of word[]
*/
void pluralize (char word[]){
/* declarations */
int length;
/* find length of word */
length = strlen(word);
/* check first rule: if word ends in "y" then change to "ies" */
if (word[length - 1] == 'y') {
word[length - 1] = 'i';
word[length] = 'e';
word[length + 1] = 's';
word[length + 2] = ''; /* remember to put '' at end of string */
}
/* check second rule: if word ends in "s" "ch" or "sh" add "es" */
else if (word[length - 1] == 's' ||
(word[length - 2] == 'c' && word[length - 1] == 'h') ||
(word[length - 2] == 's' && word[length - 1] == 'h')){
/* concatenate "es" to word */
strcat(word, "es");
}
/* otherwise, just add "s" to the end of word */
else {
strcat(word, "s");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.