字符串排序

来源:互联网 发布:北风人工智能百度云 编辑:程序博客网 时间:2024/04/28 23:02

编写一个程序,将输入字符串中的字符按如下规则排序。

规则1:英文字母从A到Z排列,不区分大小写。
如,输入:Type 输出:epTy

规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入:BabA 输出:aABb

规则3:非英文字母的其它字符保持原来的位置。
如,输入:By?e 输出:Be?y

样例:
输入:

A Famous Saying: Much Ado About Nothing (2012/8).
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

代码如下:

#include <stdio.h>#include <string.h>#include <ctype.h>void swap(char *a, char *b){char tmp;tmp = *a;*a = *b;*b = tmp;}int main(void){char str[101];//freopen("in.txt", "r", stdin);while (gets(str) != NULL){int i, j, k;int tag = 1;int len = strlen(str);/*冒泡排序稳定*/for (i=0; i<len-1 && tag==1; ++i){for (j=0; j<len-i-1; ++j){if (isalpha(str[j]) == 0) //不是字母{continue;}k = j + 1; while (isalpha(str[k])==0 && k<len){++k;}if (k == len){continue;}if (toupper(str[j]) > toupper(str[k])){swap(&str[j], &str[k]);tag = 1;}}}printf("%s\n", str);}return 0;}


 

 

0 0
原创粉丝点击