ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

字符串排序

2022-07-04 08:31:31  阅读:146  来源: 互联网

标签:opt temp int st char 字符串 排序 ct


 1 #include<stdio.h>
 2 #include<string.h>
 3 #define LIM 5
 4 #define SIZE 20
 5 
 6 char * s_gets(char *st, int n);
 7 void StrSort(char * st[], int n, int opt);
 8 
 9 int main(){
10     int ct = 0, i = 0, opt; 
11     char words[LIM][SIZE];
12     char * string[LIM];
13     
14     //输入 
15     while(ct < LIM  &&  s_gets(words[ct], SIZE) != NULL &&  words[ct][0] != '\0'){
16         string[ct] =  words[ct];
17         ct++;
18     }
19     
20     
21     //排序后输出 
22     fputs("选择0为降序,1为升序:", stdout);
23     scanf("%d", &opt);
24     StrSort(string, ct, opt);
25     for(;i < ct; i++){
26         puts(string[i]);
27     }
28     return 0;
29 }
30 
31 //为字符串按字母顺序排序
32 void StrSort(char * st[], int n, int opt){
33     int i, j;
34     char * temp;
35     
36     for(i=0; i < n - 1; i++){
37         for(j = i + 1; j < n; j++){
38             if(opt > 0){
39                 if(strcmp(st[i], st[j]) > 0){
40                     temp = st[i];
41                     st[i] = st[j];
42                     st[j] = temp;
43                 }
44             }else{
45                 if(strcmp(st[i], st[j]) < 0){
46                     temp = st[i];
47                     st[i] = st[j];
48                     st[j] = temp;
49                 }
50             }
51         }
52     }
53 }
54 
55 
56 //处理输入 
57 char * s_gets(char *st, int n){
58     fgets(st, n, stdin); 
59     char *find = strchr(st, '\n');
60     if(find){
61         *find = '\0';
62     }
63     return st;
64 } 

 

标签:opt,temp,int,st,char,字符串,排序,ct
来源: https://www.cnblogs.com/xingyboy/p/16441661.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有