字符函数和字符串函数知识总结

字符函数和字符串函数

函数介绍

  1. 求字符串长度

    1. strlen
  2. 长度不受限制的字符串函数

    1. strcpy
    2. strcat
    3. strcmp
  3. 长度受限制的字符串函数介绍

    1. strncpy
    2. strncat
    3. strncmp
  4. ?字符串查找

  5. 错误信息报告

  6. 字符分类函数

    1. islower(ch)
    2. isdigit()
    3. iscntrl()
    4. isspace()等等....
  7. 内存函数

    1. 类型为 void*
      (1).memcpy
      (2).memmove
      (3)memcmp
      (4)memset
  • strlen - 求字符串长度

int main()
{
    //int len1 = strlen("abcdef");// \0
    //char arr[] = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };//随机值
    //int len2 = strlen(arr);
    //printf("%d %d\n", len1,len2);

        //3                   //6
    if (strlen("abc") - strlen("abcdef") > 0) //结果返回类型是 unsigned 
    {
        printf("hehe\n");
    }
    else
        printf("haha\n");
    return 0;
}

  • strcpy - 字符串拷贝

#include <assert.h>
char* my_strcpy(char* dest, char* src)
{
	assert(dest != NULL);
	assert(src != NULL);
	char* ret = dest;
	//拷贝src 指向的字符串到dest指向的空间,包含‘\0‘
	while (*dest++ = *src++)// \0 阿斯克值就是 0
	{
		;
	}
	//返回目的空间的起始地址
	return ret;
}
int main()
{
	char arr1[] = "abcdefghi";
	char arr2[] = "bit";
	//错误的示范
	//char arr2[] = {‘b‘,‘i‘,‘t‘}; // 可能越界访问
	//strcpy(arr1, arr2); //连同\0拷贝
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

?

  • strcat - 末尾添加字符串

#include <assert.h>
char* my_strcat(char* dest,char* src)
{
	assert(dest != 0);
	assert(src != 0);
	while (*dest) //指向dest的末尾 \0
	{
		dest++;
	}
	while (*dest++ = *src++) //在dest最后添加src
	{
		;
	}
	char* ret = dest;
	return ret;
}
int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	//strcat(arr1, arr2); //注意目的地内存的空间必须足够够大,源字符串必须以‘\0‘结束
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

?

  • strcmp - 字符串比较

    int main()
    {
    	const char* p1 = "abcdef";
    	const char* p2 = "sqwer";
    	int ret = strcmp(p1, p2); //从首元素开始比较阿斯克码值大小
    	// > 返回 1
    	// < 返回 -1
    	// = 返回 0
    	printf("%d\n", ret);
    	return 0;
    }

?

构建my_strcmp函数

#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == ‘\0‘)
		{
			return 0;//相等
		}
		str1++;
		str2++;
	}
	if (*str1 > *str2)
		return 1;//大于
	else
		return -1; //小于

}
int main()
{
	const char* p1 = "abcdef";
	const char* p2 = "abqwe";
	int ret = my_strcmp(p1, p2);
	printf("ret = %d\n", ret);
	return 0;
}

?

  • strncpy - (受限制)

strcpy - 不受限制
int main()
{
	char arr1[5] = "abc";
	char arr2[] = "hello bit";
	strcpy(arr1, arr2); //强制拷贝
	printf("%s\n", arr1);
}

?

int main()
{
	char arr1[10] = "abcdef";
	char arr2[] = "hello";
	char arr3[] = "bit";
	strncpy(arr1, arr2, 4);
	strncpy(arr1, arr3, 4); arr3 不够4个 往后补0
	printf("%s\n", arr1);
	
	return 0;
}

?

  • strncat

int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	strncat(arr1, arr2, 3);
	printf("%s\n", arr1);
	return 0;
}

?

  • strncmp

int main()
{
	const char* p1 = "abcdef";
	const char* p2 = "abcdqwer";
	int ret = strncmp(p1, p2, 3); //比较3个字符
	return 0;
}

?

  • strstr - 查找字符串

int main()
{
	const char* p1 = "abcdef";
	const char* p2 = "def";
	const char* ret = strstr(p1, p2);
	if (ret == NULL)
	{
		printf("字串不存在");
	}
	else
		printf("%s\n", ret);
	return 0;
}

?

构建my_strstr函数

#include <assert.h>
//KMP - 算法
char* my_strstr(const char* p1, const char* p2)
{
	assert(p1 != NULL);
	assert(p2 != NULL);
	char* s1 = NULL;
	char* s2 = NULL;
	char* cur =(char*) p1;
	if (*p2 == ‘\0‘)
	{
		return (char*)p1;
	}
	while (*cur)
	{
		s1 = cur; //使s1回归
		s2 = (char*)p2;
		while (*s1 && *s2 && (*s1 == *s2));
		{
			s1++;
			s2++;
		}
		if (*s2 == ‘\0‘)
		{
			 return cur; //找到子串
		}
		/*if (*s1 == ‘\0‘)
		{
			return NULL;
		}*/
		cur++;  //若未找到 向后移一位
	}
	return NULL;//找不到子串
}

int main()
{
	const char* p1 = "abbbcdef";
	const char* p2 = "bbc";
	char* ret = my_strstr(p1, p2);
	if (ret == NULL)
	{
		printf("字串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
	}
	return 0;
}

?

  • strtok - 字符串分割

int main()
{
	//192.168.137.128
	//192 168 137 128 - strtok
	//kh@qq.com
	//kh qq com

	char arr[] = "zpw@bitede.tech";
	const char* p = "@.";
	
	char buf[1024] = { 0 };
	strcpy(buf, arr);
	char* ret = strtok(arr, p);
	printf("%s\n", ret);

	ret = strtok(NULL, p); //从‘b‘开始找
	printf("%s\n", ret);

	ret = strtok(NULL, p); //从‘t‘开始找
	printf("%s\n", ret); 
	return 0;

}
 
int main()
{
	//点分十进制的表示方式
	char arr[] = "192.168.137.128";
	const char* p = ".";

	char buf[1024] = { 0 };
	strcpy(buf, arr);

	char* ret = NULL;
	for (ret = strtok(arr, p); ret != NULL;ret = strtok(NULL,p))
	{
		printf("%s\n", ret);
	}
	return 0;
}

?

  • strerror - 返回错误码,所对应的错误信息

int main()
{
	//错误码	错误信息
	//0     -	No error
	//1		-	Operation not permitted
	//2		-	No such file or directory
	//...
	//errno 是一个全局的错误码的变量
	//当C语言的库函数在执行过程中,发生了错误,就会把对应的错误码,复制到errno中

	//char* str = strerror(0);
	//printf("%s\n", str); //No error

	//char* str = strerror(1);
	//printf("%s\n", str); //Operation not permitted

	//char* str = strerror(2);
	//printf("%s\n", str); //No such file or directory
	
	/*char* str = strerror(errno);
	printf("%s\n", str);*/

	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
	}
	else
	{
		printf("success");
	}
	return 0;
}

?

  • 字符分类函数

#include <ctype.h>
int main()
{
	//char ch = ‘w‘;
	//int ret = islower(ch); //判断是否是小写字符,返回非零整数
	//int ret = isdigit();	//十进制数字0 - 9
	//int ret = iscntrl();	//任何控制符
	//int ret = isspace();	//空白字符: 空格‘‘,换行‘\n‘ ,回车‘\r‘, 制服表‘\t‘
	//printf("%d\n", ret);

	//char ch = tolower(‘Q‘); //将Q 转换为小写q
	//char ch = toupper(‘q‘); //q -> Q
	//putchar(ch);

	char arr[] = "I Am A Student";
	int i = 0;
	//将arr转换为小写
	while (arr[i])
	{
		if (isupper(arr[i]))
		{
			arr[i] = tolower(arr[i]);
		}
		i++;
	}
	printf("%s\n", arr);
	return 0;
}

?

  • 内存函数

类型为 void*
(1)memcpy
(2)memmove
(3)memcmp
(4)memset

?

memcpy

int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[5] = { 0 };

	memcpy(arr2, arr1,sizeof(arr1));
	return 0;
}

构建my_memcpy?

#include <assert.h>
struct S
{
	char name[20];
	int age;
};

void* my_memcpy(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest != NULL);
	assert(dest != NULL);
	while (num--)
	{
		*(char*)dest = *(char*)src;
		++(char*)dest;
		++(char*)src;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[5] = { 0 };
	struct S arr3[] = { {"张三",20},{"李四",30} };
	struct S arr4[3] = { 0 };
	my_memcpy(arr4, arr3,sizeof(arr3));
	//my_memcpy(arr2, arr1, sizeof(arr1));
	
	return 0;
}

?

memmove

//C语言标准规定
//memcpy 只要处理 不重叠的内存拷贝就可以
//memmove 处理重叠内存的拷贝
 
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	//将12345拷贝到34567上
	//使结果变为 : 1,2,1,2,3,4,5,8,9,10
	int i = 0;
	//my_memcpy(arr+2,arr,20);
	memmove(arr+2, arr, 20); //处理内存重叠的情况的拷贝
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

?

构建my_memmove函数

#include <assert.h>
void* my_memmove(void* dest, const void* src, size_t count)
{
	void* ret = dest;
	assert(dest != NULL);
	assert(dest != NULL);
	if (dest < src)
	{
		//从前向后拷贝
		while (count--)
		{
			*(char*)dest = *(char*)src;
			++(char*)dest; //先强转再加加
			++(char*)src;
		}
	}
	else
	{
		//从后向前拷贝
		while (count--)
		{
			*((char*)dest + count)= *((char*)src + count); //count第一次为19
		}
	}
	return ret;
	
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int i = 0;
	//my_memcpy(arr+2,arr,20);
	my_memmove(arr + 2 , arr, 20); //处理内存重叠的情况的拷贝
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

?

memcmp

int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[] = { 1,2,5,4,3 };
	int ret = memcmp(arr1, arr2, 9); //单位字节
	printf("%d\n", ret);
	return 0;
}

?

memset - 内存设置

int main()
{
	char arr[10] = "";
	memset(arr, ‘#‘, 10); //修改arr中的10个字节改为 ‘#‘
	return 0;
}

字符函数和字符串函数知识总结

上一篇:linux安装mysql8.0


下一篇:函数返回列表