c api example

// memcmp.c

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

int main()
{
	char *s1="Hello, Programmers!";
	char *s2="Hello, programmers!";
	int r;
	char *s="Golden Global View";
	char d[20],*p;
	char *rnew="new";
	int n;

	//内存值比较
	//memicmp同memcmp的唯一区别是memicmp不区分大小写字母。
	r=memcmp(s1,s2,strlen(s1));
	if(!r)
		printf("s1 and s2 are identical");
	else
		if(r<0)
			printf("s1 less than s2");
		else
			printf("s1 greater than s2");

	//内存值复制
	memcpy(d,s,strlen(s));
	d[strlen(s)]=0;
	printf("%s",d);

	//内存复制 遇到字符ch则停止复制
	p=memccpy(d,s,'x',strlen(s));
	if(p)
	{
		*p='\0';      // MUST Do This
		printf("Char found: %s.\n",d);
	}else
		printf("Char not found.\n");

	//从buf所指内存区域的前count个字节查找字符ch。
	p=memchr(s,'P',strlen(s));
	if(p)
		printf("%s",p);
	else
		printf("Not Found!");
	//由src所指内存区域复制count个字节到dest所指内存区域。
	memmove(s,s+7,strlen(s)-7);
	s[strlen(s)-7]=0;
	printf("%s",s);

	//查找字符串s中首次出现字符c的位置
	strchr(s,'V');
	if(p)
		printf("%s",p);
	else
		printf("Not Found!");
	//在字符串s1中搜寻s2中所出现的字符。
	n=strcspn(s,rnew);
	printf("The first char both in s1 and s2 is: %c",s[n]);

	//复制字符串s
	p=strdup(s);
	printf("%s",p);
	//将字符串s转换为小写形式  strupr 转换为大写
	printf("%s",strlwr(s));

	//字符串s的所有字符的顺序颠倒过来
	printf("%s\n%s",s,strrev(strdup(s)));

	//从字符串haystack中寻找needle第一次出现的位置
	p=strstr(s,rnew);
	if(p)
		printf("%s",p);
	else
		printf("Not Found!");
	//分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
	strcpy(d," ");
	p=strtok(s,d);
	while(p)
	{
		printf("%s\n",s);
		strtok(NULL,d);
	}

	//为具有num_elems个长度为elem_size元素的数组分配内存
	p=(char *)calloc(100,sizeof(char));
	if(p)
		printf("Memory Allocated at: %x",p);
	else
		printf("Not Enough Memory!\n");

	free(p);

	p=(char *)malloc(100);
	if(p)
		printf("Memory Allocated at: %x",p);
	else
		printf("Not Enough Memory!\n");
	//改变mem_address所指内存区域的大小为newsize长度
	p=(char *)realloc(p,256);
	if(p)
		printf("Memory Reallocated at: %x",p);
	else
		printf("Not Enough Memory!\n");

	free(p);


	getchar();
	return 0;
}

你可能感兴趣的:(c api example)