...

alpha

Xiang
03-10-2003, 10:07 AM
Dear sir,

In my program, users have to enter a word and ends with a period (full-stop). If the users keys in non-alphabetical values, the message 'Error!' will be displayed and strlen(string) should not including (.). I can't displayed the output even my condition is true. Why???

void main(void)
{
char string[100];
int a;

printf("Please enter a word and ends with a full-stop(.)");
gets(string);
while(string[a] != '\0' && string[0] != '.')
{
a++;
}
if (string[--a] != '.')
{
printf("Error!");
}

else
{
if (isalpha(string[a]))
{
printf("The number of letters in a word.");
printf("%s", string);
printf("%i letter(s) in the word", strlen(string));
}
}


Thanks

Xiang

psp
03-19-2003, 05:56 AM
You are using a function isalpha in your else part.Where did u define it?First of all,no check is required there since you are already in the else part..that means the string is already acceptable.All you need to do is find its length using strlen .While printing deduct one from the computed length since it includes the period.

eggman
03-22-2003, 02:53 AM
Try the following:


while ( string[idx] != '\0' )
{
if ( !isalpha(string[ndx]) )
{
valid=0
break;
}
if ( !isspace(string[idx] )
++valid
++idx
}
if(valid==0)
printf("Error!")
else
{
printf("%s", string)
printf("\nThe length of the sentence is %i", valid)
}

The 'valid' integer is also being used as a counter of all alpha characters found in the string. Spaces are ignored. Any non-alpha character ends the while loop and displays the error message.

(My C is a bit rusty, but this looks about right.)


:confused: Did your code just change or did mine????



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum