|
alpha & space
Dear sir,
Refers to the coding below. I want to acccept alpha from the users and display the length of string excluding space. But my program even can't detected the space and if I enter the non-alpha it still can accept. Why???
If I enter the non-alpha at the beginning and center of the string or word as below, it still display the string instead of it should display the "Error!" message???
#$abc or abc%2^de
#hello world
Furthermore, if I don't want users input the word within the space, what can I do? (If users given the word within the space I want to display the error message).
void main(void)
{
char string[100];
int idx, valid;
clrscr();
printf("Enter a sentence ");
gets(string);
idx=0;
while (string[idx] != '\0')
{ if (isalpha(string[idx]) && !isspace(string[idx]))
valid = 1;
else
valid = 0;
idx++;
}
if (valid==1)
{
printf("%s", string);
printf("\nThe length of the sentence is %i", strlen(string));
}
else
printf("Error!");
Best regards,
Xiang
|