Originally Posted by
Yoonjong Kook
this is what I am trying to do
Your code reads, and considers, only one character. It tests whether that character is a digit, and a letter, and an uppercase letter. (Which is basically impossible, there is no character that is all three at the same time.)
Instead, consider reading the input into a buffer, for example using fgets(), and using a loop to check each character in it. In your lectures, you should have been told that you can define a character array as
and if you put a string in it, the maximum number of characters is one less, because the end-of-string mark (nul byte, \0) takes one. To read a line into such a buffer, use
Code:
if (fgets(line, sizeof line, stdin) == NULL) {
/* End of input! You should tell the user, then exit.
* The line array does not have anything new,
* and we should not examine it!
*/
}
Hopefully, your lecturer has also told you in the course notes that if you want to remove the newline at the end of the line -- the newline character (usually \n) is considered part of the line --, include <string.h> and you can use
Code:
line[strcspn(line, "\n")] = '\0';
which techinally removes everything starting at the first occurrence of a newline. Sometimes I use line[strcspn(line, "\r\n")] = '\0'; which removes everything starting at the first carriage return or newline, whichever occurs first. If the string in the array does not contain any of those characters, this does nothing (and is still safe to do).
Your rules can be reworded as "If there is at least one digit, and if there is at least one letter, and at least one uppercase letter, then the string is valid."
So, I suggest you set up counter variables (integers -- I'd use unsigned integers, because these counters can never be negative), and count the number of characters that are digits, the number of characters that are letters, and the number of characters that are uppercase letters.
Note that these rules are not exclusive, as the character classes may overlap: a character can be a letter, and it can be an uppercase letter. So, I would allow the sum of those counters to exceed the number of characters in the string -- check each letter for the conditions separately, one after another, but in one loop.
After checking each character in the string, base your decision on the counters.
As an added step, you could consider how you would add a new character class, say spaces and tabs (isblank()), and reject strings that have spaces or tabs in them. Or, say, more than one space or tab.
Instead of a line buffer, you could also read the input character by character, using getchar() or getc(stdin) or fgetc(stdin), and examine each character separately, but using counters like I outlined above. The difference in this case is that you don't have a char array, and instead of end of string mark, your loop stops when it encounters either newline (\0) or end-of-file mark (EOF).