Well hello again. Since the last time we met, I have gone on to learn fully about arrays and strings, and have begun to learn the basic string functions. Our problem this week has stumped me again, so I'm back to try and regain control on the subject.
The program I have here is supposed to take a singular noun, and output its plural version. Examples; toy to toys, diary to diaries, and blob to blobs.
The issue I'm having is recognizing the if...else statements in order. I had two different versions going, with an if...else within a first if statement to find y, and then determine the letter before it... However, that only either recognized the first if, or the concluding else, not the elseif between.
This time, it seems it is only recognizing the first if statement only, and ever. This one has me stumped. There are a couple other rules I have to throw in, but I'm not including them because I want to do them on my own, plus it's less to read for you all. (It's a pride / moral thing - I tried to stay away from here as long as possible, and rely on me and myself. Did good until now.
)
The program I have is as follows, and is in standard C.
The program I have here is supposed to take a singular noun, and output its plural version. Examples; toy to toys, diary to diaries, and blob to blobs.
The issue I'm having is recognizing the if...else statements in order. I had two different versions going, with an if...else within a first if statement to find y, and then determine the letter before it... However, that only either recognized the first if, or the concluding else, not the elseif between.
This time, it seems it is only recognizing the first if statement only, and ever. This one has me stumped. There are a couple other rules I have to throw in, but I'm not including them because I want to do them on my own, plus it's less to read for you all. (It's a pride / moral thing - I tried to stay away from here as long as possible, and rely on me and myself. Did good until now.
The program I have is as follows, and is in standard C.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Global variables.
int i = 0;
void plural(char *word);
int main()
{
//Variables local to main.
char word[20];
printf("Welcome. \n");
printf("Please enter a noun: ");
scanf("%s", word);
//Call to the function plural.
plural(word);
printf("%s\n", word);
return 0;
}
void plural(char *word)
{
int i = strlen(word);
//If there is a vowel before y, and if y is the last letter.
if (word[i - 2] == 'a' || 'e' || 'i' || 'o' || 'u' && word[i - 1] == 'y')
{
strcat(word, "s");
}
//If y is the last letter otherwise.
else if (word[i - 1] == 'y')
{
--i;
strcat(word, "ies");
}
//For all other possibilities. It should be an "s",
//but changed it to not confuse two "s" outputs.
else
{
strcat(word, "xxx");
}
}