How do I check if there is a special character or number in a string in C?

Asked

Viewed 4,812 times

4

I am confused in logic to check if it has other characters than alphabetic ones. I thought of the following logic, but I think it is wrong:

char nome[30];
scanf("%s", nome);
int especial = 0;
for(i=0; i<strlen(nome); i++{
  if(!(nome[i] >= 'a' && nome[i] <= 'z')){
        especial = 1; // possui caractere especial ou numero  
  }
}

The logic is correct?

  • 1

    It’s a way, but there may be better options, just with that stretch of it, anything can do. It can also be wrong because the definition is confused.

2 answers

6


Some things can be improved and I commented on the code because I did better.

From time to time I adopted the posture of teaching C in C. I think it is wrong to use C and program as if I were in another language. The question asks what can be improved and I’m pasting this.

I’ve told you a few times that should not be used strlen() when it is not necessary, this is tragic for performance, even more as condition of the loop. If this waste does not matter then it is very simple, use another language that gives more comfort.

I eliminated the flag which is almost always the wrong mechanism. I improved the condition.

If you can accept uppercase characters you have to add another valid track.

In fact, it’s possible that you wouldn’t even use that array, could manipulate a pointer and change that for for a while, seems to be the most correct solution for this code.

#include <stdio.h>

int main(void) {
    char nome[30];
    scanf("%s", nome);
    int i;
    for (i = 0; nome[i] != '\0'; i++) { //sem strlen que seria péssimo
        if (nome[i] < 'a' || nome[i] > 'z') { //lógica mais adequada
            break; //encerra o laço, não tem porque continuar, achou algo que não muda mais
        }
    }
    if (nome[i] != '\0') printf("Tem caracteres inválidos"); //se não chegou ao fim
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I kept the condition of the code because it is not clear in the question whether the intention is different from this.

  • Thank you @bigown, always very useful :D

6

In practice, this "idiomatic":

(!(nome[i] >= 'a' && nome[i] <= 'z'))

Not usually used for standardization issues.

This happens because not all languages in the world use latin alphabet in his writing.

The standard library interface ctypes.h provides several Rotinas de Classificação de Caracteres.

Are they:

int isalnum(int);: Tests whether the character is alphabetic or numidic (alphanumeric).

int isalpha(int);: Tests if the character is alphabetical.

int isascii(int);: Tests if the character is one of 128 characters of ascii table.

int isblank(int);: Tests if the character represents a space in white (space or tab).

int iscntrl(int);: Tests if the character is a command of control ASCII.

int isdigit(int);: Tests if the character is a decimal digit (0-9).

int isgraph(int);: Tests if the character is printable. (with exception of space).

int islower(int);: Tests if the character is alphabetical tiny (a-z).

int isprint(int);: Tests if the character is printable. (including space).

int ispunct(int);: Tests if the character is different from a space or any other alphanumeric character.

int isspace(int);: Tests if the character represents a space in white.

int isupper(int);: Test if the character is alphabetical capital (A-Z).

int isxdigit(int);: Tests if the character is a hexadecimal digit (0-9,A-F,a-f).

In your case, the Boolean expression:

(!(nome[i] >= 'a' && nome[i] <= 'z'))

It would be replaced simply by:

(!isalpha(nome[i]))
  • Thank you so much for the explanation, I fished on the command isalpha() later and realized that it would be better to use it even! Vlew by the help :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.