《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4
生活随笔
收集整理的這篇文章主要介紹了
《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
字符串的例子:
1.字符串排序:
應(yīng)用范圍:準(zhǔn)備花名冊,建立索引以及很多情況下都會用刀字符串的排序。這個程序的主要工具就是strcmp().
算法:讀一個字符串?dāng)?shù)組,對它們進(jìn)行排序并輸出。
#include<stdio.h> #include<string.h> #define SIZE 81 #define LIM 20 #define HALT " " void stsrt(char *strings[],int num); int main(){char input[LIM][SIZE];char *ptstr[LIM];int ct = 0 ;int k ;printf("Input up to %d lines ,and I will sort them .\n",LIM);printf("To stop,press the Enter key at a line's start .\n");while(ct < LIM &&gets(input[ct]) != NULL && input[ct][0] != '\0'){ptstr[ct] = input[ct];ct++; }stsrt(ptstr,ct);puts("\nHer's the sorted list: \n");for(k = 0 ; k < ct ; k++){puts(ptstr[k]);}return 0; } void stsrt(char *strings[],int num){char *temp;int top,seek;for(top = 0 ; top < num -1 ; top ++){for(seek = top + 1 ; seek < num ; seek++){if(strcmp(strings[top],strings[seek]) > 0){temp = strings[top];strings[top] = strings[seek];strings[seek] = temp ;}}} }
?
轉(zhuǎn)載于:https://www.cnblogs.com/yitianxi/p/5057916.html
總結(jié)
以上是生活随笔為你收集整理的《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript:改变li前缀图片和
- 下一篇: 面向对象——一起来复习托付与事件!