In: Computer Science
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:
chair dairy boss circus fly dog church clue dish
PROGRAM: C
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
// including required headers
#include <stdio.h>
#include <string.h>
int main()
{
// A character Array to store Noun
char Noun[100];
// A character Array to store Plural
char Plural[100];
// prompt for Nouns
printf("Enter a Noun: ");
// reading a string and storing it in noun
scanf("%s",Noun);
// A variable to store Length of Noun
int Length=strlen(Noun);
// If the Last character is y
if (Noun[Length-1] == 'y')
{
// Local variable
int i=0;
// Copying Noun into Plural except the last letter
for (i = 0; i < Length-1; i++)
{
// Copying each character
Plural[i] = Noun[i];
}
//Appending the word 'ies'
Plural[i++] = 'i';
Plural[i++] = 'e';
Plural[i++] = 's';
// Appending Null character
Plural[i++] = '\0';
}
// if Noun ends with 's' or 'ch' or 'sh'
else if(Noun[Length-1] == 's' ||
(Noun[Length-2] == 'c' && Noun[Length-1] == 'h') ||
(Noun[Length-2] == 's' && Noun[Length-1] == 'h')
)
{
// Local variable
int i = 0;
// Copying Noun into Plural
for (i = 0; i < Length; i++)
{
// Copying each character
Plural[i] = Noun[i];
}
//Appending the word 'ies'
Plural[i++] = 'e';
Plural[i++] = 's';
// Appending Null character
Plural[i++] = '\0';
}
// in all other cases
else
{
// Local variable
int i = 0;
// Copying Noun into Plural
for (i = 0; i < Length; i++)
{
// Copying each character
Plural[i] = Noun[i];
}
// Appending 's'
Plural[i++] = 's';
// Appending Null character
Plural[i++] = '\0';
}
//printing Noun and its Plural
printf("%s --> %s\n", Noun, Plural);
return 0;
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!