Thread: stspn, stncat

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    stspn, stncat

    i have an array which has all the alphabet characters in it and an input array and an output array. I have to use strspn and strncat in the function.
    Input = "fggh%^^hf"
    output - empty
    now i have to copy all alpha characters from input into ouput, but it doesnt seem to wokr, since strspn only uses the first occurences of alphabet characters in input so it would only take the fggh and skip the rest. My problem is how to get the rest of the letters copied into output
    so output should be fgghhf

    here is what i have tried, i hope u can help me out
    Code:
    int s = 0;
    size = strspn(input, alphabet);
    strncat(out, input, s);
    
    
            for ( i = 0; In[i] != '\0'; i++ )
            {
                    for ( j = 0; Alpha[j] != '\0'; j++ )
                    {
                            if ( Alpha[j] == In[i] )
                            {  
                             In[i] = Out[x];
                             x++;
                             break;
                            }
                    }
            }
    can someone help?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Maybe you can use the isalpha function (returns non-zero value if character is within the ranges a-z or A-Z). Just walk trough the input buffer and copy all alphabetical characters to the output buffer (don't forget to include ctype.h).

    Code:
    #include <ctype.h>
    
    x = 0;
    
    for(i = 0; In[i] != '\0'; i++)
    {
        if( isalpha(In[i]) )
        {
            Out[x] = In[i];
            x++;
        }
    }
    
    Out[x] = '\0';

Popular pages Recent additions subscribe to a feed