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.