0

I am reading a string (array of characters in C) from the user, and I am trying to analyze the number of special characters in that string. Special characters are denoted by any character in the ASCII range that is not a vowel or a blank space.

When I use this function, I get different results even when there is no string input from the user.

int restCounter (char string[]) {   
    int unsigned specialchar = 0;

    for (int i = 0; string[i] != '\0'; ++i) {
        if(string[i] != ' ' || 'a' || 'e' || 'i' || 'o' || 'u' ||'A' || 'E' || 'I' || 'O' || 'U' || '\0') {             
            specialchar++;
        }
        else if(string[i] != 'a') {            
            specialchar++;
        }
        else if(string[i] != 'e') {           
            specialchar++;
        }
        else if(string[i] != 'i') {             
            specialchar++;
        }
        else if(string[i] != 'o') {             
            specialchar++;
        }
        else if(string[i] != 'u') {          
            specialchar++;
        }
        else if(string[i] != 'A') {            
            specialchar++;
        }
        else if(string[i] != 'E') {             
            specialchar++;
        }
        else if(string[i] != 'I') {            
            specialchar++;
        }
        else if(string[i] != 'O') {         
            specialchar++;
        }
        else if(string[i] != 'U') {            
            specialchar++;
        }
        else if(string[i] != '\0') {            
            specialchar++;
        }
    }

    return specialchar; 
}
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
Aidan
  • 37
  • 1
  • 1
  • 4
  • To start: the first `if` test is always going to be true because `'a'` is always true. – Shawn Jan 26 '20 at 02:14
  • 4
    `string[i] != ' ' || 'a' || 'e' || 'i' `. C doesn't work this way. Can you use a switch statement? – Mark Plotnick Jan 26 '20 at 02:15
  • Your first `if` is invalid, as @MarkPlotnick explained. All of your others are wrong, too. For instance, `else if string[i] !- 'a'` means that a b, c, d, e, A, 1, or any other character but an `'a'` counts as a special char even if it's not, and all of your other tests will never execute. – Ken White Jan 26 '20 at 02:24

1 Answers1

1

C got a function to do precisely what you want - strchr :

#include <string.h>

unsigned int restCounter (char string[]){

unsigned int specialchar = 0;

for (size_t i = 0; string[i] != '\0'; ++i) {
     if( 0 == strchr("aeiouAEIOU ", string[i])) {
         ++specialchar;
     }
return specialchar;

}

Don't roll your own code if there is a standard library function accomplishing what you want.

However, if you don't just want to get the job done but learn by implementing your counting function manually, you got to understand how if works (in most programming languages).

if (s != 1) {
   ++special;
} else if (s != 2) {
    ++special;
}

Does not increase special only, if s is not 1 and not 2 at the same time. Just consider what happens if s is 2: the first if condition is true, this the first if body is executed and the else skipped.

Your else statements are superfluous, because they are flawed by this misunderstanding

if you want to increase special only if s is both not 1 and not 2, you need to program it just as you describe it in English:

if( (s != 1) && (s != 2)) {
  ++special;
}

Thus, if you really want to implement your counting function manually, do it like:

unsigned int restCounter (char string[]){

unsigned int specialchar = 0;

for (size_t i = 0; string[i] != '\0'; ++i) {
    char c = string[i];
    if (('a' != c) && ('e' != c) && ('A' != c) && ('E' != c)) {
    ++specialchar;
}

return specialchar;
}

It currently only uses vowels a and e, but I guess you see the pattern how to add i,o,u as well? be aware, however, that I don't advise this solution: it's is ugly, clumsy, and it's hard to figure out what it does.

Go with the first solution unless you really want to code it manually...

Btw: you won't see a '\0' in the loop body, thus checking the case c == '\0' is unnecessary.

Michael Beer
  • 853
  • 5
  • 12
  • Thanks Michael Beer! I have not explored other libraries other than the stdio. I was wrong with the logic I wanted to go with, I believe I have a better understanding of the issue I was running with. Cheers! – Aidan Jan 26 '20 at 04:34