Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Program to make singular words plural. Your assignment is to write program tha

ID: 3763713 • Letter: C

Question

C Program to make singular words plural.

Your assignment is to write program that reads words from the keyboard and makes them plural according to the following rules:

Pluralizing rules:

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 Get a word from the user.

Your program must have a minimum of 8 user defined functions (you may use additional functions,

11 suggested functions are described below):

A function that gets the word from the user o

A function that determines whether it is an all uppercase or all lowercase word //Hint: you may use the islower or isupper function from the ctype.h library o

A function that determines which rule applies to the word. //Hint: you may use 2 functions one for lowercase and one for uppercase o

A function for each rule that converts a single word //Hint: you may use 6 functions for 6 rules (3 uppercase – 3 lowercase) o

A function that saves the singular and plural words to the file. 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

Explanation / Answer

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int flag=0;
struct plural
{
   char word[10];
};
struct plural getWord()
{
   struct plural p;
   printf("Enter Word for Conversion: ");
   scanf("%s",p.word);
   return p;
}
int is_upper_case(char *w)
{
   int i;
   for (i=0;w[i]!='';i++)
   {
       if(isupper(w[i])==0)
       {
           return 0;          
       }
   }
   return 1;
}
int is_lower_case(char *w)
{
   int i;
   for (i=0;w[i]!='';i++)
   {
       if(islower(w[i])==0)
       {
           return 0;          
       }
   }
   return 1;
}
struct plural rule1(struct plural p,int ul)
{
   flag=1;
   int len=strlen(p.word);
   if(ul==1)
   {
       p.word[len-1]='I';
       p.word[len]='E';
       p.word[len+1]='S';
       p.word[len+2]='';
   }
   else
   {
       p.word[len-1]='i';
       p.word[len]='e';
       p.word[len+1]='s';
       p.word[len+2]='';
   }
   return p;
}
struct plural rule2(struct plural p,int ul)
{
   flag=1;
   int len=strlen(p.word);
   if(ul==1)
   {
       p.word[len-1]='E';
       p.word[len]='S';
       p.word[len+1]='';
   }
   else
   {
       p.word[len-1]='e';
       p.word[len]='s';
       p.word[len+1]='';
   }
   return p;
}

struct plural rule3(struct plural p,int ul)
{
   flag=1;
   int len=strlen(p.word);
   if(ul==1)
   {
       p.word[len]='E';
       p.word[len+1]='S';
       p.word[len+2]='';
   }
   else
   {
       p.word[len]='e';
       p.word[len+1]='s';
       p.word[len+2]='';
   }
   return p;
}

void write_file(struct plural p,struct plural p1)
{
  
   FILE *input, *result;
   input = fopen("input.txt","a");
   fputs(p1.word, input);
   fputs(" ", input);
   fputs(p.word, input);
   fputs(" ", input);
   fclose(input);      
}
int main()
{
   int i=1,len;
   struct plural p1,p;
   int ch=0;
    do
   {
       flag=0;
       p1=getWord();
       len=strlen(p1.word);
       if(is_upper_case(p1.word)==1 || is_lower_case(p1.word)==1)
       {
           if(p1.word[len-1]=='Y')
               p=rule1(p1,1);
           if(p1.word[len-1]=='y')
               p=rule1(p1,0);
          
           if(p1.word[len-1]=='S')
               p=rule2(p1,1);
           if(p1.word[len-1]=='s')
               p=rule2(p1,0);
          
           if(p1.word[len-2]=='S' && p1.word[len-1]=='H')
               p=rule3(p1,1);
           if(p1.word[len-2]=='s' && p1.word[len-1]=='h')
               p=rule3(p1,0);
          
           if(p1.word[len-2]=='C' && p1.word[len-1]=='H')
               p=rule3(p1,1);
           if(p1.word[len-2]=='c' && p1.word[len-1]=='h')
               p=rule3(p1,0);
           if(flag==1)
           {
               write_file(p,p1);
           }  
       }
       printf("Do you want to Take More Words: Yes=1 ");
       scanf("%d",&ch);
      
   }while(ch==1);
   return 0;
}