C语言程序设计:按字典序排序字符串

问题描述:

对分行输入的若干字符串按字典序(由小到大)进行排序并输出。

输入形式:

控制台输入多行字符串,各个字符串可能含有空格。

输出形式:

输出按字典序(由小到大)进行排序后的字符串。

样例输入:

'zwtr''fhoenf''hefe''afe geg'

样例输出:

'afe geg''fhoenf''hefe''zwtr'

样例说明:

输入多行字符串:

'zwtr''fhoenf''hefe''afe geg'

输出字典序的字符串为:

'afe geg''fhoenf''hefe''zwtr'

**代码如下:**c#include <stdio.h>#include <string.h>

int main() { char str[100][100]; int n = 0; char temp[100];

// 输入字符串 while (gets(str[n])) { n++; }

// 排序 for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (strcmp(str[i], str[j]) > 0) { strcpy(temp, str[i]); strcpy(str[i], str[j]); strcpy(str[j], temp); } } }

// 输出排序后的字符串 for (int i = 0; i < n; i++) { printf('%s ', str[i]); }

return 0;}

C语言程序设计:按字典序排序字符串

原文地址: https://www.cveoy.top/t/topic/omcI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录