C PROGRAM HELP!!! Your assignment is to write program that reads words from the
ID: 3762295 • Letter: C
Question
C PROGRAM HELP!!!
Your assignment is to write program that reads words from the keyboard and makes them plural according to the following rules:
Rules
1.If the noun ends in ‘y’, remove the ‘y’ and add “ies” to the end.Examples: cherry becomes cherries
2.If the noun ends in ‘Y’, remove the ‘Y’ and add “IES” to the end.Examples: PUPPY becomes PUPPIES
3.If the noun ends with the letters ‘s’, “ch”, or “sh” then simply add the letters “es” to the end Examples: bus becomes buses
4.If the noun ends with the letters ‘S’, “CH”, or “SH” then simply add the letters “ES” to the end Examples: CHURCH becomes CHURCHES
5.If the rules above do not apply then just add the letter ‘s’ to the end of the word. Examples: Mushroom becomes mushrooms If the rules above do not apply then just add the letter ‘S’ to the end of the word.
6.Examples: PINEAPPLE becomes PINEAPPLES
• your program will read ask the user if they want to enter a word, then read the word from the keyboard You may use scanf(“%s”, word); //word is a character array of size 20
•After reading each word your program will create the plural form of the word, display it on the screen save the original input word to a file and save the plural form to the same file.
•Your program will repeatedly read in words in English and output singular and plural words to the file until the user decides to quit
•You may assume that all input will be typed in alphabetic characters and that no punctuation or numeric characters will be used. You don't need to check this you can simply assume that the input will only contain the letters A through Z
(the whole word will be input as either all UPPERCASE or all lowercase letters) and no white space in the words.
Explanation / Answer
/*
* File: newmain.c
* Author: Krish
*
* Created on November 13, 2015, 9:30 PM
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define max_word 20
void pluralize(char word[]);
int main(void) {
char noun[max_word]; // input noun singular
for (;;) {
printf("Enter a noun in singular form: or done to terminate ");
scanf("%s", noun);
// enter done to stop giving input
if (strcmp(noun, "done") == 0 || strcmp(noun, "DONE") == 0)
break;
pluralize(noun);
printf("The plural form is %s ", noun);
}
return;
}
void pluralize(char word[]) {
int length;
char noun;
length = 1;
length = strlen(word);
// if ending with y, removes y and adds ies
if (word[length - 1] == 'y') {
word[length - 1] = 'i';
word[length] = 'e';
word[length + 1] = 's';
word[length + 2] = '';
}
// if ending with Y, removes Y and adds IES
else if (word[length - 1] == 'Y') {
word[length - 1] = 'I';
word[length] = 'E';
word[length + 1] = 'S';
word[length + 2] = '';
}
/* if word ends in "s" "ch" or "sh" add "es" */
// if ending with s/ch/sh, adds es
else if (word[length - 1] == 's' || (word[length - 2] == 'c' && word[length - 1] == 'h') || (word[length - 2] == 's' && word[length - 1] == 'h')) {
strcat(word, "es");
}// if ending with S/CH/SH adds ES
else if (word[length - 1] == 'S' || (word[length - 2] == 'C' && word[length - 1] == 'H') || (word[length - 2] == 'S' && word[length - 1] == 'H')) {
strcat(word, "ES");
}
// if last char is in upper case, add 'S' else 's' (small case)
else if (isupper(word[length - 1])) {
strcat(word, "S");
} else {
strcat(word, "s");
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.