This question is for C programming.

I have a function that takes a string and returns the plural version of the string. For example, if I passed the function the string “chair” it would return “chairs”. My question is, is there a way to pass the original string to the function Plural and NOT have the original string be modified? As you will see in my code, I want to first print the plural version of the word by passing the original string to the function Plural, and then I want to print the non-plural version of the word by just using the original string and a %s placeholder. However, this doesn't work since the original string gets modified by the function call to Plural. The only thing I can think of to do to not modify it is first make a copy of the original string in the main function, then pass the copy to the function plural. However, in this case I would not be passing the original string to the function; I would be passing the copy, and I'm wondering if there is a way to do this without using a copy. If there is, please show me the code on how to do it.

#include
#include
#define STRSIZ 20

char *Plural(char *noun);
int main(void) {

   char temp[15] = “chair”;
  
   /*this will print “chairs”*/
   printf(“%sn”, Plural(temp));
  
   /*want it to print “chair”, but it prints “chairs” since original string was modified”*/
   printf(“%s”, temp);
   return 0;
}
char *Plural(char *noun) {

   int len = strlen(noun);

   if (noun[len – 1] == 'y') {
       strcpy(&noun[len – 1], “ies”);
   }
   else if (noun[len – 1] == 's' ||
       (noun[len – 2] == 's' && noun[len – 1] == 'h') ||
       (noun[len – 2] == 'c' && noun[len – 1] == 'h')) {
       strcat(noun, “es”);
   }
   else {
       strcat(noun, “s”);
   }
   return noun;
}

"Get 15% discount on your first 3 orders with us"
Use the following coupon
"FIRST15"

Order Now