求解一个C语言问题,如下 ?
在屏幕上随便输入一行字符串,然后按照ASCII排序从小到大输出到屏幕上。示例:
Please Enter Strings:
2395nAg
Sort Result Is:
2
3
5
9
g
n
A
do you want to continue?(Y\N)
Y--->退出
N--->重复
Please Enter Strings:
root@~ #cat 15.c #include <stdio.h> int main (void) { char str[80],*ptr=str,*p=ptr,temp; int i,j; while(1) { gets(ptr); for(i=0;*(ptr+i)!='\0';i++) { for(j=i+1;*(ptr+j)!='\0';j++) { if(*(ptr+i)>*(ptr+j)) { temp=*(ptr+i); *(ptr+i)=*(ptr+j); *(ptr+j)=temp; } } } while(*ptr) printf("%c\n",*ptr++); printf("Press Y continue , any key to exit: "); temp=getchar(); getchar(); if(temp!='y') break; } return 0; } root@~ #./15 asdf a d f s Press Y continue , any key to exit: y 3wer 3 e r w Press Y continue , any key to exit: n root@~ #
#include <stdio.h> #include <string.h> main(void){ char str[100],flagStr[100] = "N"; int i,j,k; char temp; do{ if(strcmp(flagStr,"N")==0){ printf("Please Enter Strings:"); scanf("%s",&str); for(i=0;i<strlen(str);i++){ k = i; for(j=i;j<strlen(str);j++){ if(str[i]>str[j]){ k = j; } } if(k!=i){ temp = str[i]; str[i] = str[k]; str[k] = temp; } } //输出 for(i=0;i<strlen(str);i++){ printf("%c\n",str[i]); } } printf("do you want to continue?(Y\N)\n"); printf("Y--->退出\nN--->重复\n"); scanf("%s",flagStr); strupr(flagStr); }while(strcmp(flagStr,"Y")!=0); return 0; }