C语言函数介绍

1.字母的大小写转换-->islower()

2.快速排序函数-->qsort()

下面正式给大家介绍这两个函数

(1)islower()

islower() 函数用来检测一个字符是否是小写字母。

在默认情况下,小写字母包括:

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 库函数 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) 对数组进行排序。

需要头文件:#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 ValueDescription
< 0elem1 less than elem2
0elem1 equivalent to elem2
> 0elem1 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 “less than” in the comparison function.

 举个实例:

题目描述:

期中考试开始了,大家都想取得好成绩,争夺前五名。从键盘输入n个学生成绩(不超过40个),输出 每组排在前五高的成绩。

输入描述:

两行,第一行输入一个整数,表示n个学生(>=5),第二行输入n个学生成绩(整数表示,范围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]);
   }
    //对所有数字排序
    int j = 0;
   //使用库函数排序
    qsort(score, n, 4, cmp_int);
     for(i=0; i<5; i++)
   {
        printf("%d ", score[--n]);
   }
    return 0;
}

以上就是今天的分享了,对你有帮助,点个关注谢谢。

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴趣使然的Qsiri

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值