I have a Program in C I need help with Overview Write a program that takes nouns
ID: 3685709 • Letter: I
Question
I have a Program in C I need help with
Overview
Write a program that takes nouns and forms their plurals on the basis of these rules:
If noun ends in "y", remove the "y" and add "ies"
If noun ends in "s", "ch", or "sh", add "es"
In all other cases, just add "s".
Print each noun and its plural. Try the following data:
Requirements
The program must accept 1 or more program arguments on the command line.
The program must pluralize all words provided as input
The program must print out the original word and the pluralized version.
You may assume all arguments are nouns to begin with.
Hints
Refer to the Bonus Program lab for information about accepting command-line program arguments.
Write functions to break the big problem into smaller problems. For example:
Pluralize one word at a time instead of worrying about them all together
Checking if a string ends with a certain suffix
Remeber to include the string.h library. You'll probably want to use these string functions:
strlen
strcpy
strcat
Example Output
This is what i have
#include <stdio.h>
#include <string.h>
#define max_word 25
/* prototypes */
void pluralize (char word[]);
int main (void)
{
char noun[max_word]; /* stores temporary word entered by user */
for( ; ; ){
printf("noun :");
scanf("%s", noun);
if ((strcmp(noun, "done")) == 0)
break;
pluralize (noun);
printf("plural: %s ", noun);
}
}
void pluralize (char word[])
{
int length;
char noun;
length=1;
length = strlen(word);
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" */
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");
}
else
{ strcat(word, "s");
printf("New word is: ", &noun);
}
return;
}
Explanation / Answer
Solution.c
#include <stdio.h>//header file for input put put functions
#include <string.h>//header file used for string functions
#define maximum_words 50//maximun words up to 50
/* prototypes *///function declaration
void pluralize_words (char word[]);
void main (void)
{
char noun[maximum_words];
printf("Enter a noun in singular form: ");
scanf("%s", noun);//keyboard inputting
for (;;)
{
scanf("%s", noun);
if (strcmp(noun, "done") == 0)
break;
pluralize_words (noun);//function calling
printf("The plural form is %s ", noun);
}
}
void pluralize_words (char word[])
{//function definition
int len;
char noun;
len=1;
len = strlen(word);
if (word[len - 1] == 'y')
{ word[len - 1] = 'i';
word[len] = 'e';
word[len + 1] = 's';
word[len + 2] = '';
}
/* if word ends in "s" "ch" or "sh" add "es" */
else if (word[len - 1] == 's' ||
(word[len - 2] == 'c' && word[len - 1] == 'h') ||
(word[len - 2] == 's' && word[len - 1] == 'h'))
{ strcat(word, "es");
}
else
{ strcat(word, "s");
printf("", &noun);
}
}
output
This program prints the plural form of a noun.
Please type in a noun in singular form: chair dairy boss circus fly dog church clue dish
The plural form is chairs
The plural form is dairies
The plural form is bosses
The plural form is circuses
The plural form is flies
The plural form is dogs
The plural form is churches
The plural form is clues
The plural form is dishes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.