void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) 对数组进行排序。
声明
下面是 qsort() 函数的声明。
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
参数
- base -- 指向要排序的数组的第一个元素的指针。
- nitems -- 由 base 指向的数组中元素的个数。
- size -- 数组中每个元素的大小,以字节为单位。
- compar -- 用来比较两个元素的函数。
返回值
该函数不返回任何值。
实例
下面的实例演示了 qsort() 函数的用法。
#include <stdio.h>
#include <stdlib.h>
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int n;
printf("排序之前的列表:\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\n排序之后的列表:\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
return(0);
}
让我们编译并运行上面的程序,这将产生以下结果:
排序之前的列表:
88 56 100 2 25
排序之后的列表:
2 25 56 88 100
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) 对 nitems 对象的数组执行二分查找,base 指向进行查找的数组,key 指向要查找的元素,size 指定数组中每个元素的大小。
数组的内容应根据 compar 所对应的比较函数升序排序。
声明
下面是 bsearch() 函数的声明。
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
参数
- key -- 指向要查找的元素的指针,类型转换为 void*。
- base -- 指向进行查找的数组的第一个对象的指针,类型转换为 void*。
- nitems -- base 所指向的数组中元素的个数。
- size -- 数组中每个元素的大小,以字节为单位。
- compar -- 用来比较两个元素的函数。
返回值
如果查找成功,该函数返回一个指向数组中匹配元素的指针,否则返回空指针。.
实例
下面的实例演示了 bsearch() 函数的用法。
实例
#include <stdio.h>
#include <stdlib.h>
int cmpfunc(const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int values[] = { 5, 20, 29, 32, 63 };
int main ()
{
int *item;
int key = 32;
/* 使用 bsearch() 在数组中查找值 32 */
item = (int*) bsearch (&key, values, 5, sizeof (int), cmpfunc);
if( item != NULL )
{
printf("Found item = %d\n", *item);
}
else
{
printf("Item = %d could not be found\n", *item);
}
return(0);
}
让我们编译并运行上面的程序,这将产生以下结果:
Found item = 32
C语言 isalpha() 函数用来检测一个字符是否是字母,包括大写字母和小写字母
头文件:ctype.h
语法/原型:
int isalpha(int c);
参数 c 表示要检测字符或者 ASCII 码。
返回值:返回非 0(真)表示 c 是字母,返回 0(假)表示 c 不是字母。
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char str[] = "C++ Java C#";
while (str[i])
{
if (isalpha(str[i])) {
printf("%c is alphabetic\n", str[i]);
}
else {
printf("%c is not alphabetic\n", str[i]);
}
i++;
}
return 0;
}
如果需要对字符串中的单个字符进行查找,那么应该使用 strchr 或 strrchr 函数。
其中,strchr 函数原型的一般格式如下:
char *strchr(const char *s, int c);
它表示在字符串 s 中查找字符 c,返回字符 c 第一次在字符串 s 中出现的位置,如果未找到字符 c,则返回 NULL。
也就是说,strchr 函数在字符串 s 中从前到后(或者称为从左到右)查找字符 c,找到字符 c 第一次出现的位置就返回,返回值指向这个位置,如果找不到字符 c 就返回 NULL。
C 库函数 double atof(const char *str) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
声明
下面是 atof() 函数的声明。
double atof(const char *str)
参数
- str -- 要转换为浮点数的字符串。
返回值
函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。
实例
下面的实例演示了 atof() 函数的用法。
实例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float val;
char str[20];
strcpy(str, "98993489");
val = atof(str);
printf("字符串值 = %s, 浮点值 = %f\n", str, val);
strcpy(str, "runoob");
val = atof(str);
printf("字符串值 = %s, 浮点值 = %f\n", str, val);
return(0);
}
让我们编译并运行上面的程序,这将产生以下结果:
字符串值 = 98993489, 浮点值 = 98993488.000000 字符串值 = runoob, 浮点值 = 0.000000