#include <string.h>文件函数详解

#include <string.h>文件函数详解

1.strlen()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
//此部分为直接调用string库里的strlen()函数
//int main() {
//	char *str="hello";
//	printf("%d\n",strlen(str));
//	return 0;
//}

//	strlen实现机理
int mylen(char *str);
int main(int argc,char *argcment) {
	char *str="hello world!";
	int length=	mylen(str);
	printf("%d",length);
}
int mylen(char *str) {
// char *str="hello";
	int index=0;
	while(str[index]!='\0') {
		index++;
	}
// printf("index=%d",index);
	return index;
}

2.strcmp()

在这里插入代码片
#include <stdio.h>
#include <string.h>

//直接调用string的strcmp()函数
int main(int argc,char *argv[]){
	char *a="hello ";
	char *b="hello";
	printf("%d",strcmp(a,b));
	
	return 0;
} 

//实现机理
//int mycmp(char *a,char *b);
//int main(int argc,char *argv[]) {
//	char *a="hello ";
//	char *b="hello";
//	int ret=mycmp(a,b);
//	if(ret==0) {
//		printf("same");
//	} else if(ret>0) {
//		printf("a more big");
//	} else {
//		printf("b more big");
//	}
//	return 0;
//}
//int mycmp(char *a,char *b) {
//	//method 1,利用数组的方式
	int idx=0;
	while(a[idx]==b[idx]&&a[idx]!=0){
		idx++;
	}
	return a[idx]-b[idx];
//
method 2,利用指针的方式
//	while(*a==*b &&*a!='\0') {
//		a++;
//		b++;
//	}
//	return *a-*b;
//}

3.strcpy()

在这里插入代码片
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//实现机理 
char* mystr(char* dst,char* str) {
	int idx=0;
//	while(str[idx]!='\0'){
//		dst[idx]=str[idx];
//		idx++;
//	}
//	dst[idx]='\0';
//	return dst;
//指针
	char *rest=dst;
	while(*str!='\0') {
		*dst=*str;
		str++;
		dst++;
	}
	*dst='\0';
	return rest;
}

int main(int argc,char const *argv[]) {

	char* s1="abc";
	char* s2=(char*)malloc(strlen(s1)+1);
	mystr(s2,s1);
	printf("%s",s2);
	free(s2);
	return 0;
}

4.strcat()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//int main(int argc,char* argv[]){
//	//注意s1的空间必须足够大 
//	char s1[10]="abc";
//	char* s2="-hello";
//	strcat(s1,s2);
//	printf("%s",s1);
//	return 0;
//}

//实现机理 
char *mycat(char* s1,char*s2);
int main(int argc,char* argv[]){
	char s1[20]="hello";
	char* s2="abcdef";
	mycat(s1,s2);
	printf("%s",s1);
	return 0;
} 

char *mycat(char* s1,char*s2){
	//数组 
//	int idx=0;
//	while(s1[idx]!='\0'){
//		idx++;
//	}
//	int idx2=0;
//	while(s1[idx2]!='\0'){
//		s1[idx]=s2[idx2];
//		idx++;
//		idx2++;
//	}
//	return s1;

//指针
char* ret=s1;
//s1的长度是5,strlen(s1)=5,也就是s2添加到hello后面即复制后是helloabcdef
//strcpy(s1+strlen(s1),s2);中的s1+strlen(s1)将要复制的字符串从s1+5位置开/始放置
//如果是strcpy(s1,s2);则s1变成s2了,就不是连接了而是替换了
strcpy(s1+strlen(s1),s2);
return ret; 
}

5.strchr()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[]) {
	char s[]="hello";
	char *p=strchr(s,'l');
	
	//正常获取 
	printf("%s\n",p);
	
	//获取he;
	char c=*p;
	*p='\0';
	char *t=(char*)malloc(strlen(s)+1);
	strcpy(t,s);
	printf("%s",t);
	free(t);
	*p=c;
	
	// 
	return 0;
}
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include<sys/types.h> #include<sys/wait.h> int g_i4event; int process_wait(void) { /********Begin********/ /*********End*********/ } int process_waitpid(void) { /********Begin********/ /*********End*********/ }这个代码根据下面要求写试想这样的场景: 你和你的树懒一起到外面吃午餐,你吃的比较快,但是要等待树懒吃完后才能一起走。 整个过程如下: 你和树懒开始就餐; 你需要等待树懒吃完后再一起走。 以进程等待代替整个过程, 对于上述过程可以理解为创建子进程->等待子进程退出->主进程退出。 本关任务: 创建子进程; 等待子进程退出。 相关知识 在 Linux 系统中,进程的生命周期内主要包含:进程就绪、进程执行、进程等待和进程退出。 就绪转执行 处于就绪状态的进程,当进程调度程序为之分配了处理机(CPU)后,该进程便由就绪状态转变成执行状态。 执行转就绪 处于执行状态的进程在其执行过程中,因分配给它的一个时间片已用完或更高优先级的进程抢占而不得不让出处理机,于是进程从执行状态转变成就绪状态。 执行转等待 正在执行的进程因等待某种事件发生而无法继续执行时,便从执行状态变成等待状态。 等待转就绪 处于阻塞状态的进程,若其等待的事件已经发生,于是进程由等待状态转变为就绪状态。 执行转退出 进程执行完毕,撤销而退出。 其中,进程等待是进程控制的关键,也是本关的重点。 进程等待的原因 **进程控制块(PCB)**是进程在整个运行过程中非常重要的一个数据结构,里面包含以下几种信息: 进程描述信息; 进程控制信息; 资源信息; 现场保护信息。 进程终止或者退出的时候,进程会关闭所有文件描述符,释放用户空间分配的内存,但是PCB却会暂时保留。假如进程正常终止, PCB 里面就记录进程的退出状态;如果是异常终止,那么 PCB 里面存放着导致该进程终止的信号。 子进程的退出状态必须由父进程回收,也就是说,父进程必须得等待子进程退出后再自己退出,否则子进程会变成僵尸进程(没有任何可执行代码,不会被调度,只有一个进程描述符用来记录退出的状态,除此之外不再占用其他任何资源)。 wait函数 #include<sys/types.h> #include<sys/wait.h> pid_t wait(int* status); 参数: status ,整型指针,指向的地址即存放退出子进程的退出状态,不关心可以设为 NULL。 等待方式是阻塞式等待,一旦子进程退出,父进程就会立刻收到 SIGCHLD 信号。 注意:父进程调用 wait,是等待任意一个已经退出的子进程,只要是有子进程退出了,那么就获取子进程退出状态然后返回。 应用示例: #include <sys/wait.h> int main() { pid_t pid = fork(); ...... if(0 == id) { /*子进程代码区域*/ sleep (5); } else (id > 0) { /*父进程代码区域*/ int status = 0; wait(&status); } return 0; ...... } waitpid函数 #include<sys/types.h> #include<sys/wait.h> pid_t waitpid( pid_t pid, int *status ,int options); 参数详解: pid:希望等待退出的子进程的进程号; status:整型指针,指向的地址即存放退出子进程的推出状态,不关心可以设为 NULL; options:设置为 0,代表阻塞式等待,如果设置为 WNOHANG, waitpid 发现没有已经退出的子进程可以收集,就返回 0,此时是非阻塞式等待。 返回值: 返回 0 表示没有已经退出的子进程;返回正值,表示有已经退出的子进程。 编程要求 在主函数的最开始会初始化一个全部变量g_i4event为0。 本关的编程任务是补全右侧代码片段中两段Begin至End中间的代码,具体要求如下: 创建子进程,创建完后,将 g_i4event 置为 1; 子进程等待 3s ,然后子进程退出; 父进程等待一秒,将 g_i4event 置为 2;然后等待子进程退出,父进程获取子进程退出的状态后将 g_i4event 置为 3; process_wait采用 wait 函数,process_waitpid 采用 waitpid 函数。 测试说明 测试过程: 用户补全框架内的代码,实现功能性代码; 接着根据程序的输出判断程序是否正确。 不要在代码中使用printf等进行输出,以免引起结果判断错误。 以下是测试样例: 测试输入:1 预期输出: You and Sloth go to have lunch! You have finished your meal!You need wait the sloth Sloth have finished your meal! You and sloth will go home. 测试输入:2 预期输出: You and Sloth go to have lunch! You have finished your meal!You need wait the sloth Sloth have finished your meal! You and sloth will go home. 向今天献出自己的人,没有哪一个昨天是给浪费掉的。 开始你的任务吧,祝你成功! 如果你觉得这一关的内容对你有帮助,请你在下面点赞。
最新发布
05-16
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "ImageInverse.h" #include "bmp_head_struct.h" image_inverse_create_t inverse_create; image_inverse_frame_t inverse_frame; image_inverse_state_t inverse_state; BITMAPFILEHEADER *p_bmpFileHead; // 14 B BITMAPINFOHEADER *p_bmpInfoHead; // 40 B void *handle; /* * main.c */ int main(void) { int SizeImage; int width,height; FILE *file_src = NULL; FILE *file_dst = NULL; BYTE *p_src = NULL, *p_dst = NULL; clock_t begin,end; printf("---Image Inverse Processing!\n"); file_src = fopen("..//bmps//HazyDay1.bmp","rb"); if (!file_src){ printf("file_src open error!\n"); return -1; } file_dst = fopen("..//bmps//HazyDay1_inverse.bmp","wb"); if (!file_dst){ printf("file_dst open error!\n"); return -1; } p_bmpFileHead = (BITMAPFILEHEADER *)malloc(14); p_bmpInfoHead = (BITMAPINFOHEADER *)malloc(40); #if 1 //条件编译 fread(p_bmpFileHead, 1, 14, file_src); #else fread(&p_bmpFileHead->bfType,1,2,file_src); fread(&p_bmpFileHead->bfSize,1,4,file_src); fread(&p_bmpFileHead->bfReserved1,1,2,file_src); fread(&p_bmpFileHead->bfReserved2,1,2,file_src); fread(&p_bmpFileHead->bfOffBits,1,4,file_src); #endif fread(p_bmpInfoHead, 1, 40, file_src); width = p_bmpInfoHead->biWidth; height= p_bmpInfoHead->biHeight; SizeImage = width*height*3; p_src = (BYTE *)malloc(SizeImage); p_dst = (BYTE *)malloc(SizeImage); if ((!p_src) || (!p_dst)){ printf("error in malloc for image data!\n"); return -1; } //1.-----------------------------------------------------------------create inverse_create.width = width; inverse_create.height= height; begin = clock(); image_inverse(NULL, DSP_ALG_CREATE, &inverse_create, NULL); end = clock(); //printf("DSP_ALG_CREATE = %d",end - begin); handle = inverse_create.handle; fread(p_src, 1, Size
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值