字符串比较大小 并且去重

来源:互联网 发布:mac电脑磁盘在哪里 编辑:程序博客网 时间:2024/06/04 19:35

输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照字典序输出这些单词,要求重复的单词只输出一次。(区分大小写)

输入
一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。数据不含除字母、空格外的其他字符。
输出
按字典序输出这些单词,重复的单词只输出一次。
样例输入
She  wants  to go to Peking University to study  Chinese
样例输出
ChinesePekingSheUniversitygostudytowants




//此题因为各种小错误 ,博主纠结了两个小时,最后找出错误的感觉真的很爽。

//二维数组传参比较重要

//Geeksun 2017.12.18

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char (*p)[60],int n);
int delete_str(char (*p)[60],int n);
int main()
{
    int i = 0,count1 = 0,count2 = 0;
    char word1[120][60],word2[121][60],temp[60];
    while(scanf("%s",temp) != EOF)
    {
        if(temp[0] >= 65&&temp[0] <=90)
        {
            strcpy(word1[count1++],temp);
        }
        else if(temp[0] >= 97&&temp[0] <=122)
        {
            strcpy(word2[count2++],temp);
        }
        if(getchar() == '\n')
        {
            break;
        }
    }
    sort(word1,count1);
    sort(word2,count2);
    count1 = delete_str(word1,count1);
    count2 = delete_str(word2,count2);
    for(i = 0;i < count1;i++)
    {
        printf("%s\n",word1[i]);
    }
    for(i = 0;i < count2;i++)
    {
        printf("%s\n",word2[i]);
    }
    return 0;
}
void sort(char (*p)[60],int n)
{
    int i,j;
    char temp[60];
    for(i = 0;i < n - 1;i++)
    {
        for(j = 0;j < n - i - 1;j++)
        {
            if(strcmp(p[j],p[j + 1]) > 0)
            {
                strcpy(temp,p[j]);
                strcpy(p[j],p[j + 1]);
                strcpy(p[j + 1],temp);
            }
        }
    }
}
int delete_str(char (*p)[60],int n)
{
    int i,j;
    for(i = 0;i < n - 1;i++)
    {
        if(strcmp(p[i],p[i + 1]) == 0)
        {
           for(j = i;j < n - 1;j++)
           {
               strcpy(p[j],p[j + 1]);
           }
           i--;
           n--;
        }
    }
    return n;
}


原创粉丝点击