当前位置:   article > 正文

【经典C程序】给出的字符串排字母序_c语言字母排序编程

c语言字母排序编程

在这里插入图片描述

vim alpha_order.c

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

int main(void)
{

        int i, j, num;
        char name[20][20], t_name[15][10], temp[20];

        printf("Please enter how many number of names to be sorted in alphabetical order.\n");
        scanf("%d", &num);

        printf("Please enter %d names one by one\n", num);

        for (i = 0; i < num; i++)
        {
                scanf("%s", name[i]);
                strcpy(t_name[i], name[i]);
        }

        for (i = 0; i < num - 1; i++)
        {
                for (j = i + 1; j < num; j++)
                {
                        if(strcmp(name[i], name[j]) > 0)
                        {
                                strcpy(temp, name[i]);
                                strcpy(name[i], name[j]);
                                strcpy(name[j], temp);
                        }


                }
        }

        printf("Names before sorting in alphaetical order\n");
        for (i = 0; i < num; i++)
        {
                printf("%s      ", t_name[i]);
        }
        printf("\n");

        printf("Names after sorting in alphaetical order:\n");
        for (i = 0; i < num; i++)
        {
                printf("%s      ", name[i]);
        }

        printf("\n");

        return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
Please enter how many number of names to be sorted in alphabetical order.
5
Please enter 5 names one by one
one two three four five
Names before sorting in alphaetical order
one     two     three   four    five
Names after sorting in alphaetical order:
five    four    one     three   two
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/651001
推荐阅读
相关标签
  

闽ICP备14008679号