C语言函数介绍
1.字母的大小寫轉換-->islower()
2.快速排序函數(shù)-->qsort()
下面正式給大家介紹這兩個函數(shù)
(1)islower()
islower() 函數(shù)用來檢測一個字符是否是小寫字母。
在默認情況下,小寫字母包括:
| a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z |
?
檢測到是小寫字母時可以將它轉換為大寫字母,具體我們可以用一個實例來看看。
1.KiKi想完成字母大小寫轉換,有一個字符,判斷它是否為大寫字母,如果是,將它轉換成小寫字母;反 之則轉換為大寫字母。
輸入描述:?多組輸入,每一行輸入一個字母。
輸出描述:針對每組輸入,輸出單獨占一行,輸出字母的對應形式
示例1
輸入
a
A
Z
輸出
A
a
z
參考代碼:
#include <stdio.h> int main() {int ch = 0;//多組輸入while((ch=getchar()) != EOF){if(islower(ch)){printf("%c\n", toupper(ch));}else{printf("%c\n", tolower(ch)); }//處理'\n'getchar();}return 0; }這里強調一下,toupper()的意思是將小寫字母轉換為大寫字母,tolower()的意思是將大寫字母轉換為小寫字母!
(2)qsort()
C 庫函數(shù)?void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))?對數(shù)組進行排序。
需要頭文件:#include<stdlib.h>或#include<search.h>
具體介紹如下:
?
Remarks
The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:
compare( (void *) elem1, (void *) elem2 );
The routine must compare the elements, then return one of the following values:
| Return Value | Description |
| < 0 | elem1 less than elem2 |
| 0 | elem1 equivalent to elem2 |
| > 0 | elem1 greater than elem2 |
The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “l(fā)ess than” in the comparison function.
?舉個實例:
題目描述:
期中考試開始了,大家都想取得好成績,爭奪前五名。從鍵盤輸入n個學生成績(不超過40個),輸出 每組排在前五高的成績。
輸入描述:
兩行,第一行輸入一個整數(shù),表示n個學生(>=5),第二行輸入n個學生成績(整數(shù)表示,范圍0~100),用 空格分隔。
輸出描述:
一行,輸出成績最高的前五個,用空格分隔。
示例:
輸入:
6
99 45 78 67 72 88
輸出
99 88 78 72 67
參考代碼:
#include <stdio.h> int cmp_int(const void* e1, const void*e2) {return *(int*)e1 - *(int*)e2; } int main() {int n = 0;int score[40] = {0};scanf("%d", &n);int i = 0;for(i=0; i<n; i++){scanf("%d", &score[i]);}//對所有數(shù)字排序int j = 0;//使用庫函數(shù)排序qsort(score, n, 4, cmp_int);for(i=0; i<5; i++){printf("%d ", score[--n]);}return 0; }以上就是今天的分享了,對你有幫助,點個關注謝謝。
總結
- 上一篇: 使用Java实现的高精度科学计算器
- 下一篇: 李开复给中国大学生的第三封信——成功、自