F - Andy's First Dictionary

F - Andy's First Dictionary


题意:对一段文章中单单词进行排序后输出。按字典排序。

题解:1.将单个单词从从文章中提取出来存到字符数组中。2.对单词进行排序。

提取单词的时候一个字符一个字符提取,遇到非单词字符则转到下一个单词提取。

排序用qsort快速排序,有个cmp函数是指排序方式。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

char word[1000001][205];

int cmp(const void * s1,const void * s2)
{
    return strcmp((char *)s1, (char *)s2);
}

int main()
{
    char c;
    int i = 0,j = 0;
    while((c = getchar()) != EOF)
    {
        if(c <= 'Z' && c >= 'A')
        {
            word[i][j++] = c + 32;
        }
        else if(c <= 'z' && c >= 'a')
        {
            word[i][j++] = c;
        }
        else if(j)
        {
            word[i++][j] = '\n';
            j = 0;
        }
    }
    qsort(word,i,sizeof(word[0]),cmp);
    for(int x = 0;x < i;x++)
    {
        if(strcmp(word[x - 1],word[x]))
        printf("%s",word[x]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值