字符串按大小进行排序

xiaoxiao2021-02-28  89

#include <stdio.h> #include <string.h> int main() { char str[10][15]; //10个字符串 char *p[10]; char buf[15]; int n; int i,j; printf("请输入字符串的个数:"); scanf("%d",&n); getchar(); for(i=0;i<n;i++) { p[i]=str[i]; gets(p[i]); } for(i=0;i<n-1;i++) { for(j=0;j<n-1-i;j++) { if((strcmp(p[j],p[j+1])==1)) { strcpy(buf,p[j]); strcpy(p[j],p[j+1]); strcpy(p[j+1],buf); } } } printf("---------------\n"); for(i=0;i<n;i++) puts(p[i]); }
转载请注明原文地址: https://www.6miu.com/read-64062.html

最新回复(0)