SF1747 Apps Downned n thing Analytical R we CIS April 10, 2017 1 Question 1 Writ
ID: 3818776 • Letter: S
Question
SF1747 Apps Downned n thing Analytical R we CIS April 10, 2017 1 Question 1 Write a function: vaid plurals char nnum, char at takes An input word rmoun) and output ita plural (plural the is of the e rules sh", add In all other eases, just add "s" Hiuta: given the string noun, you should dirst check its length L by using strlen function. ld chock if the last lett y If yes, should copy the first L you should deck if al, alld sh" and ake th In all other should just to plural and then d. Y add null symbol after thi ast let torr plural string. Using the plurals functiu d reads it from put 2. Paser the strime to function plural, which finds its pla 3. Prints out the ural strin. 4. Irompts the muscr to do another by typing "v or to quit the program hy typing anything else. Sample output: Supply Dour:chair The plural is: chairs Do Another (yin)?v The plural is: dairies Do Another iyiny?vExplanation / Answer
// C code
#include <stdio.h>
#include <string.h>
void plurals(char* noun, char *plural)
{
int i;
int n=strlen(noun); //taking the length of noun
if(noun[n-1] == 'y')
{
for (i = 0; i < n-1; ++i)
{
plural[i] = noun[i];
}
plural[i++] ='i';
plural[i++] ='e';
plural[i++] ='s';
plural[i] ='';
}
else if(noun[n-1] == 's' || (noun[n-2] == 'c' && noun[n-1] == 'h') || (noun[n-2] == 's' && noun[n-1] == 'h'))
{
for (i = 0; i < n; ++i)
{
plural[i] = noun[i];
}
plural[i++] ='e';
plural[i++] ='s';
plural[i] ='';
}
else
{
for (i = 0; i < n; ++i)
{
plural[i] = noun[i];
}
plural[i++] ='s';
plural[i] ='';
}
}
int main()
{
char plural[30];
char noun[30]; // string declared to store the input noun
char end[10];
while(1)
{
printf(" Supply noun: ");
scanf("%s",noun); //string input
plurals(noun,plural);
printf("The Plural is %s ", plural);
getchar();
printf("Do another(y/n): ");
scanf("%s",end);
if(strcmp(end,"n")==0) //when string is "q" , quit
{
break;
}
}
return 0;
}
/*
output:
Supply noun: chair
The Plural is chairs
Do another(y/n): y
Supply noun: dairy
The Plural is dairies
Do another(y/n): n
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.