-2

I have to write this program where when the user enters a word, it changes it to the plural version of it. I have three rules.

  1. if it ends in a ‘y’, we change it to ‘ies’
  2. if it ends in “sh” or “ch”, we add “es” to the end
  3. Else, We add a ‘s’ at the end

I have this function named WhichRule( char word[], char plural []), I am supposed to write if statements so I can see which rule to apply, the function WhichRule will call RuleOne( char word[], char plural []), RuleTwo( char word[], char plural []), and RuleThree( char word[], char plural []).

I am having trouble writing my if statements, the code im going to copy is two different ways in which I’ve tried to write my if statements, but none have worked. I would appreciate any help.

void WhichRule(char word[], char plural[])
{
    int len = strlen(word);

    if (word[len - 1] = 'y')
    {
        ApplyRuleOne(plural, word);
        printf("this");
    }
    else if (word[len - 2, len - 1] = 'c', 'h')
    {
        ApplyRuleTwo(plural, word);
        printf("is");
    }
    else if (word[len - 2] == 's' && word[len - 1] == 'h')
    {
        ApplyRuleTwo(plural, word);
        printf("a");
    }
    else
    {
       ApplyRuleThree(plural, word);
       printf("test");
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    `word[len - 1] == 'y'` equality not assignment and `word[len - 2, len - 1] = 'c', 'h'` --> `word[len-2]=='c' && word[len-1]=='h'`. – user2736738 Nov 18 '17 at 19:10
  • 1
    `else if (word[len - 2, len - 1] = 'c', 'h')` - what on earth is this? – Steve Nov 18 '17 at 19:11
  • You have a comma operator in your `else-if`, and again assignment instead of checking equality. – Tony Tannous Nov 18 '17 at 19:11
  • And the expression `word[len - 2, len - 1] = 'c', 'h')` assigns an `'h'` to `word[len-1]`.... you have a couple [comma operators](https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do) in that expresion there – Ray Toal Nov 18 '17 at 19:12

1 Answers1

2

Problem

Here is a common mistake of confusing '=' with '=='. '=' is assignment operator ans is used to assign value to a variable So

if (word[len - 1] = 'y')

This will assign the value 'y' to variable word[len-i]

Solution

To compare the values, use '==' like

if (word[len - 1] == 'y')

Code

void WhichRule(char word[], char plural[])
{
    int len = strlen(word);

    if (word[len - 1] == 'y')
    {
        ApplyRuleOne(plural, word);
        printf("this");
    }
    else if (word[len - 2] == 'c' && word[len - 1] == 'h')
    {
        ApplyRuleTwo(plural, word);
        printf("is");
    }
    else if (word[len - 2] == 's' && word[len - 1] == 'h')
    {
        ApplyRuleTwo(plural, word);
        printf("a");
    }
    else
    {
       ApplyRuleThree(plural, word);
       printf("test");
    }
}

[P.S : Also there was problem with the line that checks for last characters 'ch'.]

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
H. Sodi
  • 540
  • 2
  • 9
  • A residual problem is that if you pass `"a"` as the word, the code tries to access the character before the beginning of the word, which is undefined behaviour. – Jonathan Leffler Nov 18 '17 at 20:04