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

4

I'm confused in logic to see if you have other characters than alphabetic characters. I thought 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  
  }
}

Is the logic correct?

    
asked by anonymous 11.07.2017 / 00:04

2 answers

6

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

Some time ago I adopted the posture of teaching to program C in C itself. I think it's wrong to use C and program as if in another language. The question asks what can be improved and I'm pasting it.

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

I've deleted the flag which is almost always the wrong engine. I improved the condition.

If you can accept upper case characters you will have to add another valid range.

In fact it is possible that you could not even use this array counter, could manipulate a pointer and change that for to while , seems to be the most correct solution for this code. p>

#include <stdio.h>

int main(void) {
    char nome[30];
    scanf("%s", nome);
    int i;
    for (i = 0; nome[i] != '
#include <stdio.h>

int main(void) {
    char nome[30];
    scanf("%s", nome);
    int i;
    for (i = 0; nome[i] != '%pre%'; 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] != '%pre%') printf("Tem caracteres inválidos"); //se não chegou ao fim
}
'; 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] != '%pre%') printf("Tem caracteres inválidos"); //se não chegou ao fim }

See running on ideone . And at Coding Ground . Also put it in GitHub for future reference .

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

    
11.07.2017 / 00:42
6

In practice, this " idiomatic :

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

It is not usually used for standardization purposes.

This happens because not all languages in the world use the Latin alphabet in your writing.

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

They are:

  

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

     

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

     

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

     

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

     

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

     

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

     

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

     

int islower(int); : Tests whether the character is lowercase alphabetic   (a-z).

     

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

     

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

     

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

     

int isupper(int); : Tests whether the character is uppercase alphabetic   (A-Z).

     

int isxdigit(int); : Tests whether 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 simply be replaced by:

(!isalpha(nome[i]))
    
11.07.2017 / 15:22