liaohuongxing 2016-02-13 15:30 采纳率: 100%
浏览 2425
已采纳

菜鸟求助!按字典顺序排列问题

输入若干英文单词,将每个单词的首字母转换成大写字母,其他字母为小写,并按字典顺序排列

  • 写回答

5条回答 默认 最新

  • threenewbee 2016-02-13 22:37
    关注
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int cmp(const void * a, const void * b)
    {
        return strcmp(*(char **)a, *(char **)b);
    }
    
    int main(int argc, char* argv[])
    {
        int n = 0;
        int i;
        printf("how many words?\n");
        scanf("%d", &n);
        char ** s = new char *[n];
        for (i = 0; i < n; i++)
        {
            s[i] = new char[100];
            scanf("%s", s[i]);
            char * t = s[i];
            while (*t != '\0')
            {
                if (t == s[i] && (*t >= 'a' && *t <= 'z')) *t = *t - 'a' + 'A';
                if (t > s[i] && (*t >= 'A' && *t <= 'Z')) *t = *t - 'A' + 'a';
                t++;
            }
        }
        qsort(s, n, sizeof(char *), cmp);
        for (i = 0; i < n; i++)
        {
            printf("%s\n", s[i]);
        }
        return 0;
    }
    
     how many words?
    5
    wORd
    HellO
    yEllow
    she
    APPLE
    Apple
    Hello
    She
    Word
    Yellow
    Press any key to continue
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试