Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: alpha

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    alpha

    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

  • #2
    psp
    psp is offline
    New Coder
    Join Date
    Mar 2003
    Location
    CA
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try the following:

    Code:
    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.)


    Did your code just change or did mine????
    Last edited by eggman; 03-22-2003 at 02:56 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •