参考代码如下:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char str[100];
    gets(str);

    char* words[20]; //存放分割的字符型指针
    int idx = 0;

    //以空格符分割字符串,存放至字符指针数组中
    char *token = strtok(str, " ");
    while (token != NULL)
    {
        words[idx++] = token;
        token = strtok(NULL, " ");
    }

    //将该字符指针数组各元素所指向的字符串进行比较并排序
    int i, j;
    for (i = 0; i < idx - 1; i++)
    {
        for (j = i + 1; j < idx; j++)
        {
            if (strcmp(words[i], words[j]) > 0)
            {
                char *tmp = words[i];
                words[i] = words[j];
                words[j] = tmp;
            }
        }
    }

    int num = 0;
    for (i = 1; i < idx; i++)
    {
        if (strcmp(words[i], words[i - 1]) != 0)
            num++;
    }

    if (num != 0)
        printf("%d\n", num + 1);
    else
        printf("%d\n", num);

    return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区