A 'switch' checks multiple options for a single variable (int or char). In this case, you're checking str1[length-1], thus:
1 2 3
|
switch(str1[length-1]) {
// Code comes here
}
|
Now, a switch checks that variable for several pre-defined values, called 'cases'. In this situation, each possible letter is a 'case'. For example:
case 'x': strcat(str1,"es");
However, this means every letter has a separate case, meaning 26 cases! However, you can combine cases. The thing is: a switch() case doesn't stop executing until it reaches a "break;" statement. That means it will execute the code from the next case if there is no break!
As a result, you can do:
1 2 3 4 5
|
case 'x':
case 'h':
case 's':
cat(str1,"es");
break;
|
Now all three cases are combined, saving you the effort of copypasting code (and making your code that much sexier).
Just a few pointers, though:
a) You're not checking for out-of-bounds problems. What if I type in a word that's 19 characters long and you want to glue 'es' behind it?
b) In the case of 'f': you're glueing "ves" behind it, but you're not deleting the 'f'! I'm not sure how to fix it (I don't work with C strings, sorry), but you should definitely look that up.
c) Your if statements were pretty faulty... You should definitely practice those. You can't do "x == 'y' || 'z'". You need to do "x == 'y' || x == 'z'". Else, you'll always get "true".