Thread: Split numbers and alpha

  1. #1
    Unregistered
    Guest

    Angry Split numbers and alpha

    need help!! how can i split digit from alpha from user input

    I want to extract numbers and place them into char digit and print all numbers then do the same thing for alpha and print alpha separatly .

    I am a biginner c student .

    here is what i did so far.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    int main(void)
    {
    char t[80];
    char line[80];
    char alpha[80];
    char digit[80];
    int k;

    printf("enter something: ");
    gets(line);

    strcpy(t,line);
    puts(t);
    for (k=0;k<strlen(t);k++)
    {

    if (isalpha(t[k]));
    strcpy(alpha,t[k]);

    if (isdigit(t[k]));
    strcpy(digit,t[k]);
    }
    puts(alpha);
    puts(digit);
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( string[x] )
    {
        if( isdigit( string[x] ) )
        {
            digit[y++] = string[x];
        }
        else
        if( isalpha( string[x] ) )
        {
            alpha[z++] = string[x];
        }
        x++;
    }
    Assuming this is what you want, here's an easy implementation.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest

    Smile

    thanks...It works a bit sluggish but it is much better from what I came up with...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How exactly is it sluggish? Even if you were using strchr and then copying chunks out at a time, you still have to go through a single character at a time. Sluggish? I think not. This is probably *THE FASTEST* implementation you're going to get. There is nothing sluggish about it.

    You have no overhead from unneeded function calls. You have at most 4 checks per loop and one assignment. I'd like to see it implemented faster. The only way to speed this up would be to replace the 'isdigit' / 'isalpha' checks with simple mathmatic comparisons.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    the only way i see quzah to speed this up other than mentioned is some(possiblly complicated) pointer arithmetic, but the speed gain would for the most part be negligible.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Unregistered
    Guest
    sorry dude...but perhaps sluggish was not the right word.
    what i meant is that the program keeps on putting the number 2 after I print alpha. ex: fhdgshjk2

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    sorry dude...but perhaps sluggish was not the right word.
    what i meant is that the program keeps on putting the number 2 after I print alpha. ex: fhdgshjk2
    Well then you're doing it wrong.

    char alpha[BUFZIS] = {0};
    char digit[BUFSIZ] = {0};
    char string[BUFSIZ] = {0};

    ... read ...
    ... loop ...

    printf("Alpha: %s\n", alpha );
    printf("Digit: %s\n", digit );

    If you're calling all of this in a loop, then you need to zero value your strings when you're done with them. Use memset or something similar.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementing alpha characters. Base36
    By chops11 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2006, 02:56 PM
  2. Validating Numbers only
    By shacapoo in forum C Programming
    Replies: 25
    Last Post: 12-11-2004, 11:13 PM