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
Beginning PHP and MySQL 5
W. Jason Gilmore / Apress / 2006-01-23 / USD 44.99
Beginning PHP and MySQL 5: From Novice to Professional, Second Edition offers comprehensive information about two of the most prominent open source technologies on the planet: the PHP scripting langua......一起来看看 《Beginning PHP and MySQL 5》 这本书的介绍吧!