字符串解析为单词

#include
#include
#include
#include

#define N 20
#define M 30


int str_parase(char * res, char word[][N])
{
    int i, wordLen=0, wordNum=0;
    char temp[20];
    int n = strlen(res);

    for (i = 0; i < n; i++)
    {
        if (res[i] == ' ' || res[i] == '\t')
        {
            if (wordLen != 0)
            {
                temp[wordLen] = '\0';
                strcpy_s(word[wordNum], temp);
                wordNum++;
                wordLen = 0;
            }
        }
        else
        {
            temp[wordLen++] = res[i];
        }
    }
    if (word != 0)
    {
        temp[wordLen] = '\0';
        strcpy_s(word[wordNum], temp);
        wordNum++;
        wordLen = 0;
    }

    return wordNum;
}

int main()
    {
        char b[M][N];
        int num;
        int i;
        char s[] = "what are you doing ?";


        num = str_parase(s, b);

        if (0 == strcmp(b[0], "what"))
            printf("OK\n");

        printf("num : %d \n", num);
        for (i = 0; i < num; i++)
            printf("%s\n", b[i]);

        while (1);
        return 0;





    }




你可能感兴趣的:(C语言)