C 语言实例 - 字符串排序

C 语言教程 · 2019-02-21 10:11:31

按字典顺序排序。

实例

#include<stdio.h> #include <string.h> int main() { int i, j; char str[10][50], temp[50]; printf("输入10个单词:\n"); for(i=0; i<10; ++i) scanf("%s[^\n]",str[i]); for(i=0; i<9; ++i) for(j=i+1; j<10 ; ++j) { if(strcmp(str[i], str[j])>0) { strcpy(temp, str[i]); strcpy(str[i], str[j]); strcpy(str[j], temp); } } printf("\n排序后: \n"); for(i=0; i<10; ++i) { puts(str[i]); } return 0; }

输出结果为:

输入10个单词:
C
C++
Java
PHP
Python
Perl
Ruby
R
JavaScript
PHP

排序后: 
C
C++
Java
JavaScript
PHP
PHP
Perl
Python
R
Ruby

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Algorithms and Theory of Computation Handbook

Algorithms and Theory of Computation Handbook

Mikhail J. Atallah (Editor) / CRC-Press / 1998-09-30 / USD 94.95

Book Description This comprehensive compendium of algorithms and data structures covers many theoretical issues from a practical perspective. Chapters include information on finite precision issues......一起来看看 《Algorithms and Theory of Computation Handbook》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具