字符串函数

常用三类系统函数:1)字符串;2)时间;3)数学

1)头文件<string.h>,找C标准库参考手册看即可,里面包含众多

C 标准库 – <string.h> | 菜鸟教程 (runoob.com)

此处提供一个链接仅供参考。

例子:

#include <stdio.h>
#include <string.h>

void main(){
    char src[50], dest[50];
    char* str = "abcdef";
    printf("str.len=%d",strlen(str));   //统计字符串大小
    strcpy(src, "hello");   //把hello拷贝到src,abcdef被hello覆盖
    strcpy(dest, "你好");   //把你好拷贝到dest
    strcat(dest ,src);  //把src连接在dest后面
    printf("\n最终字符串为:|%s|",dest);
}

 

2)头文件<time.h>,附带参考标准库链接

C 标准库 – <time.h> | 菜鸟教程 (runoob.com)

例子:

1 #include <stdio.h>
2 #include <time.h>
3 
4 int main(){
5     time_t curtime; //time_t是一个结构体
6     time(&curtime); //time()完成初始化
7     printf("当前时间=%s",ctime(&curtime));  //ctime返回一个表示当地时间的字符串,当地时间是基于参数timer
8     return 0;
9 }

==============================================================================================================

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 void test(){    //运行test函数,求所花费的时间
 5     int i = 0;
 6     int sum = 0;
 7     int j = 0;
 8     for(i=0;i<7777777;i++){
 9         sum = 0;
10         for(j=0;j<100;j++){
11             sum +=j;
12         }
13     }
14 }
15 
16 int main(){
17     //得到test执行前的时间
18     time_t start_t, end_t;
19     double diff_t;  //存放时间差
20     printf("启动\n");
21     time(&start_t); //初始化得到当前时间
22     test(); //test执行
23     //得到test执行后的时间
24     time(&end_t);   //得到到当前时间
25     diff_t = difftime(end_t, start_t);  //时间差
26     printf("执行test函数用了%.6f秒", diff_t);
27 }

请注意,数值取决于您的电脑性能,性能较差别设太大!


 

3)头文件<math.h>,附参考链接

C 标准库 – <math.h> | 菜鸟教程 (runoob.com)

 

 注意:

 

posted @ 2022-10-07 15:45  Morning枫  阅读(96)  评论(0)    收藏  举报