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 2 of 2

Thread: alpha & space

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

    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

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could you give some input and output so that we might better understand?

    Jason


  •  

    Posting Permissions

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