Greetings everyone. I need your help creating a plural.c program. I will give a
ID: 3607464 • Letter: G
Question
Greetings everyone.
I need your help creating a plural.c program. I will give a great rate for your answer. Remember the program is C language and inlvolves these 8 prototypes:
//A function that greets the user
void Greeting();
//A function that gets the word from the user
void GetWord(char[]);
//A function that changes the word to all uppercase
//or all lowercase letters
void WordUpper(char word[]);
//A function that determines which rule applies to the word.
void WhichRule(char word[], char plural[]);
//applies rule one
void ApplyRuleOne(char word[], char plural[]);
//applies rule 2
void ApplyRuleTwo(char word[], char plural[]);
//applies rule 3
void ApplyRuleThree(char word[], char plural[]);
//adds the word to the file
void AddToFile(FILE *outPtr, char word[], char plural[]);
*these are the instructions:
Your assignment is to write program that scans words from the keyboard and makes them plural and saves both the original form of the word and the plural form in an output file called
pluralWords.txt
Rules
1. If the word ends in ‘y’, remove the ‘y’ and add “ies” to the end.
Examples: cherry becomes cherries
2. If the word ends with the letters ‘s’, “ch”, or “sh” then simply add the letters “es” to the end Examples: bus becomes buses, church becomes churches
3. If the above rules above do not apply then just add the letter‘s’ to the end of the word. Examples: Mushroom becomes mushrooms
Your program will 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 either an all upper or all lower version of the word and make a copy of that word.
Next your program will create the plural form of the word and display it on the screen.
Next your program will save the original input word (that is all uppercase or all lowercase), to a file and also save the plural form in 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 (by entering n)
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 and no white space in the words. You do NOT need to verify whether or not the user entered a real word or a “nonsense” word.
Additional minimal requirements:
Your program must have a minimum of 8 user defined functions (you may use additional functions)
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<ctype.h>
#include<string.h>
//A function that greets the user
void Greeting();
//A function that gets the word from the user
void GetWord(char[]);
//A function that changes the word to all uppercase
//or all lowercase letters
void WordUpper(char word[]);
//A function that determines which rule applies to the word.
void WhichRule(char word[], char plural[]);
//applies rule one
void ApplyRuleOne(char word[], char plural[]);
//applies rule 2
void ApplyRuleTwo(char word[], char plural[]);
//applies rule 3
void ApplyRuleThree(char word[], char plural[]);
//adds the word to the file
void AddToFile(FILE *outPtr, char word[], char plural[]);
*HERE Sample Output:
Sample output for program 3:
********* Welcome to the Pluralizer *********** The output file pluralWords.txt is open ----------------------------------------
Would you like to enter a word Y (YES) or N (NO)? y
Enter a word funny
Rule is 1
Word is FUNNY and plural is FUNNIES
Adding the words to the file
Would you like to enter a word? Y (YES) or N (NO)y
Enter a word bunch
Rule is 2
Word is BUNCH and plural is BUNCHES
Adding the words to the file
Would you like to enter a word? Y (YES) or N (NO)y
Enter a word COLLege
Rule is 3
Word is COLLEGE and plural is COLLEGES
Adding the words to the file
Would you like to enter a word? Y (YES) or N (NO)y
Enter a word FISH
Rule is 2
Word is FISH and plural is FISHES
Adding the words to the file
Would you like to enter a word? Y (YES) or N (NO)n
Thank you for trying out the Pluralizer!
Closing the file pointer
Press any key to continue . . .
Explanation / Answer
#include<stdio.h>
#include<ctype.h>
#include <string.h>
#include <stdbool.h>
// Have to declare it global to use everywhere
FILE *fptr;
bool endsWith (char* base, char* str) {
int blen = strlen(base);
int slen = strlen(str);
return (blen >= slen) && (0 == strcmp(base + blen - slen, str));
}
//A function that greets the user
void Greeting() {
fprintf(fptr, "********* Welcome to the Pluralizer *********** The output file pluralWords.txt is open ---------- ");
fprintf(fptr, "------------------------------ ");
}
//A function that gets the word from the user
void GetWord(char word[]) {
scanf("%s", word);
}
//A function that changes the word to all uppercase
//or all lowercase letters
void WordUpper(char word[]) {
int sizeOfWord = strlen(word);
for(int i=0;i<sizeOfWord;i++) {
if(word[i] >='a' && word[i] <='z') {
word[i] = word[i] -'a' + 'A';
}
}
}
//applies rule one
void ApplyRuleOne(char word[], char plural[]) {
// printf("%s",word);
int sizeOfWord = strlen(word);
strncpy(plural, word, sizeOfWord-1);
strncpy(plural+sizeOfWord-1,"ies", 3);
plural[sizeOfWord+2] = '';
}
//applies rule 2
void ApplyRuleTwo(char word[], char plural[]) {
int sizeOfWord = strlen(word);
strncpy(plural, word, sizeOfWord);
strncpy(plural+sizeOfWord,"es", 2);
plural[sizeOfWord+2] = '';
}
//applies rule 3
// first copies word to plural, copies additional string as well
// do not forget to end the string with character
void ApplyRuleThree(char word[], char plural[]) {
int sizeOfWord = strlen(word);
strncpy(plural, word, sizeOfWord);
strncpy(plural+sizeOfWord,"s", 1);
plural[sizeOfWord+1] = '';
}
//A function that determines which rule applies to the word.
void WhichRule(char word[], char plural[]) {
if(endsWith(word,"y")) {
fprintf(fptr, "Rule is 1 ");
ApplyRuleOne(word, plural);
} else if(endsWith(word,"s") || endsWith(word,"ch") || endsWith(word,"sh")) {
fprintf(fptr, "Rule is 2 ");
ApplyRuleTwo(word,plural);
} else {
fprintf(fptr, "Rule is 3 ");
ApplyRuleThree(word,plural);
}
}
//adds the word to the file
void AddToFile(FILE *outPtr, char word[], char plural[]) {
fprintf(outPtr, "Word is %s and plural is %s ", word, plural);
}
int main() {
// open file with write permissions
fptr = fopen("pluralWords.txt","w");
char plural[256], word[256];
Greeting();
char yesOrNo;
fprintf(fptr, "Would you like to enter a word? Y (YES) or N (NO)y ");
while(scanf(" %c",&yesOrNo) == 1 && yesOrNo == 'y') {
fprintf(fptr, "Enter a word ");
GetWord(word);
fprintf(fptr, "word %s ", word);
WhichRule(word,plural);
AddToFile(fptr, word, plural);
fprintf(fptr, "char is %c", yesOrNo);
fprintf(fptr, "Would you like to enter a word? Y (YES) or N (NO)y ");
fflush(stdin);
}
fprintf(fptr, "Thank you for trying out the Pluralizer! ");
fprintf(fptr, "Closing the file pointer ");
fclose(fptr);
fprintf(fptr, "Press any key to continue . . . ");
getc(stdin);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.