0

I am getting an int of the entire string s for 'letter', the conditions in my 'if' statement seem to not be reading properly - is my syntax incorrect?

I get user input:

string s = get_string("Text here:  ");

the function is as follows:

int letter_count(string s)
    {
        int i =0;
        int len = strlen(s);
        int letter = 0;
        
        while(i < len)
        { 
        if (s[i] != '\0' || s[i] != '.' || s[i] != ',' || s[i] != '!' || s[i] != '?')
            {
                letter++;
            }
            i++;
        } 
        return letter;
    }

then call the function:

int letter = letter_count(s);
printf("letter Count: %i\n", letter);
  • Please read [ask] and provide [mre], your output and the desired output. – Yunnosch Aug 12 '21 at 06:01
  • Your boolean logic is wrong. Make a habit of reading it loud: "if the character is not null OR the character is not dot OR..." For anything that isn't the null terminator, only the first condition will get evaluated since it is true. Also, it is a better idea to simply call `isalpha`. – Lundin Aug 12 '21 at 09:06
  • Example: `int letter_count (const char* s) { int letters = 0; for(; *s!='\0'; s++) letters += isalpha(*s); return letters; }` – Lundin Aug 12 '21 at 09:08

3 Answers3

1

Try changing the OR operator with the AND

GodDoesBAU
  • 59
  • 3
  • Somebody found this answer without either code or explanation more useful than the other answers. I have to admit that this surprises me. But (anonymous) voting is generally appreciated as a fundamental part of this community. So thanks for voting, I guess. – Yunnosch Aug 12 '21 at 06:49
0

if (s[i] != '\0' || s[i] != '.' || s[i] != ',' || s[i] != '!' || s[i] != '?')

is ALWAYS true. Because any character is either not "." or not ",". Which letter would you expect to be both?

You want to check whether the current letter is "not ." AND "not ," AND "not !".
I.e.
if (s[i] != '\0' && s[i] != '.' && s[i] != ',' && s[i] != '!' && s[i] != '?')

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

Almost correct, you have to change the type of the argument to char*, there is no string type on the default libraries. (Documentation of string library).

Working example with the modifications:

#include <stdio.h>
#include <string.h>

int letter_count (char* s)
{
  int i = 0;
  int len = strlen (s);
  int letter = 0;

  while (i < len)
    {
      if (s[i] != '\0' && s[i] != '.' && s[i] != ',' && s[i] != '!'
      && s[i] != '?')
    {
      letter++;
    }
      i++;
    }
  return letter;
}

int main ()
{
  char my_word[] = "Sample word";
  printf ("'%s' have %d letters",my_word, letter_count (my_word));

  return 0;
}

Output:

'Sample word' have 11 letters
susanth29
  • 356
  • 1
  • 2
  • 17
  • Try "...four..!!". It should of course result in 4. Your code will have an incorrect result. If you disagree please explain what your condition `if (s[i] != '\0' || s[i] != '.' || s[i] != ',' || s[i] != '!' || s[i] != '?')` achieves and why you disagree with OP that it is the core problem of the function. – Yunnosch Aug 12 '21 at 06:08
  • You're right. The 'or' must to be changed to 'and' ```#include #include int letter_count (char* s) { int i = 0; int len = strlen (s); int letter = 0; while (i < len) { if (s[i] != '\0' && s[i] != '.' && s[i] != ',' && s[i] != '!' && s[i] != '?') { letter++; } i++; } return letter; } int main () { char my_word[] = "...Four..!!"; printf ("'%s' have %d letters",my_word, letter_count (my_word)); return 0; }``` This works fine. – Armando Leopoldo Keller Aug 12 '21 at 06:12
  • Are you aware that you can [edit] your posts? – Yunnosch Aug 12 '21 at 06:14
  • "there is no string type on the default libraries" Are you sure about that for the quite unique "cs50" environment? – Yunnosch Aug 12 '21 at 06:22
  • @susanth29 While I technically agree with you, doing a functionally fundamental code change like that is widely considered beyond what an edit should do to the post of a different user. I am surprised that your proposal was accepted. This kind of edits should be handled by the answerers themselves. – Yunnosch Aug 12 '21 at 06:25
  • 1
    @Yunnosch noted – susanth29 Aug 12 '21 at 06:26