...

View Full Version : alpha & space



Xiang
03-15-2003, 07:00 PM
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

Jason
03-18-2003, 12:13 AM
Could you give some input and output so that we might better understand?

Jason



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum