字符函数和字符串函数(1)

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组 中。字符串常量适用于那些对它不做修改的字符串函数。

目录

函数介绍

strlen

 strcpy

strcat

strcmp

strncpy

strncat

strncmp

strstr

strtok

strerror


函数介绍

strlen

size_t strlen ( const char * str );

1.字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

2.参数指向的字符串必须要以 '\0' 结束。
3.注意函数的返回值为size_t,是无符号的( 易错 )
4.学会strlen函数的模拟实现

为了说明3,举个例子:

这里输出大于看起来很奇怪,原因就在于strlen的返回值是size_t类型,是无符号数,因此,-3在内存中的原码被当做无符号数,emmm 

//-3

原码:10000000000000000000000000000011

反码:1111111111111111111111111111111111100

补码:1111111111111111111111111111111111101,这个补码会被当做无符号数看待,无符号数九很大了,远大于0!

 模拟实现strlen

方法1:计数器

#include <stdio.h>
size_t my_strlen(char* ps)
{
	size_t count = 0;

	while (*ps != '\0')
	{
		count++;
		ps++;
	}

	return count;
}
int main()
{
	size_t sz = my_strlen("abcdef");
	printf("%u\n",sz);

	return 0;
}

 方法2:指针-指针

#include <stdio.h>
size_t my_strlen(char* ps)
{
	char* start = ps;
	while (*ps)
	{
		ps++;
	}
	return ps - start;
	
}
int main()
{
	size_t sz = my_strlen("abcdef");
	printf("%u\n",sz);

	return 0;
}

 方法3:递归

#include <stdio.h>
size_t my_strlen(char* ps)
{
	if (*ps != '\0')
	{
		return 1 + my_strlen(ps + 1);
	}
	else
		return 0;

}
int main()
{
	size_t sz = my_strlen("abcdef");
	printf("%u\n",sz);

	return 0;
}

 strcpy

char* strcpy(char * destination, const char * source );

1.Copies the C string pointed by source into the array pointed by destination, including the
terminating null character (and stopping at that point).
2.源字符串必须以 '\0' 结束。
3.会将源字符串中的 '\0' 拷贝到目标空间。
4.目标空间必须足够大,以确保能存放源字符串。
5.目标空间必须可变。
6.学会模拟实现

模拟实现strcpy

#include <assert.h>
char* my_strcpy(char* dest, char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(dest != NULL);
	while (*dest++ = *src++)
	{
		;
	}

	return ret;
}

strcat

char * strcat ( char * destination, const char * source );

1.Appends a copy of the source string to the destination string. The terminating null character
in destination is overwritten by the first character of source, and a null-character is included
at the end of the new string formed by the concatenation of both in destination.
2.源字符串必须以 '\0' 结束。
3.目标空间必须有足够的大,能容纳下源字符串的内容。
4.目标空间必须可修改。
5.字符串自己给自己追加,如何?

 模拟实现strcat

#include <string.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
	assert(src && dest);
	char* ret = dest;
	//1.找目标空间的\0
	while (*dest)
	{
		dest++;
	}
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "world";
	my_strcat(arr1, arr2);
	printf("%s\n",arr1);
	return 0;
}

特别提醒:不要自己给自己追加(例如:strcat(arrr1,arr1))!

strcmp

int strcmp ( const char * str1, const char * str2 );

1.This function starts comparing the first character of each string. If they are equal to each
other, it continues with the following pairs until the characters differ or until a terminating
null-character is reached.
2.标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字

模拟实现strcmp 

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == *str2)
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;

}

int main()
{
	int ret = my_strcmp("abc","abc");
	printf("%d\n",ret);
	return 0;
}

 总结:strcpy、strcat、strcmp是长度不受限制的字符串函数,接下来说几个长度受限制的字符串函数:strncpy、strncat、strncmp

strncpy

1.Copies the first num characters of source to destination. If the end of the source C string
(which is signaled by a null-character) is found before num characters have been copied,
destination is padded with zeros until a total of num characters have been written to it.
2.拷贝num个字符从源字符串到目标空间。
3.如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个

strncat

char * strncat ( char * destination, const char * source, size_t num );

Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating
null-character is copied

#include <stdio.h>
#include <string.h>
int main ()
{
    char str1[20];
    char str2[20];
    strcpy (str1,"To be ");
    strcpy (str2,"or not to be");
    strncat (str1, str2, 6);
    puts (str1);
    return 0;
}

strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

int main()
{
	char arr1[] = "abcqwertyuiop";
	char arr2[] = "abcdef";
	printf("%d\n", strncmp(arr1, arr2, 4));

	return 0;
}

strstr

char * strstr ( const char *str1, const char * str2);

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of
str1

int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",6);
puts (str);
return 0;
}

模拟实现strstr

char* my_strstr(const char* str1, const char* str2)
{
	char* cp = str1;
	char* s1 = str1;
	char* s2 = str2;

	while (*cp)
	{
		s1 = cp;
		s2 = str2;
		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return cp;
		cp++;
	}
	return NULL;

}

int main()
{
	char arr1[] = "abbbcdef";
	char arr2[] = "bbc";
	char* ret = my_strstr(arr1, arr2);
	if (ret != NULL)
		printf("%s\n", ret);
	else
		printf("找不到\n");
	return 0;
}

strtok

char * strtok ( char * str, const char * sep );

1.sep参数是个字符串,定义了用作分隔符的字符集合
2.第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。

3.strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
4.strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
5.strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
6.如果字符串中不存在更多的标记,则返回 NULL 指针

int main()
{
	char arr[] = "apeng@yuii.net@666";
	char copy[30];
	strcpy(copy, arr);

	char sep[] = "@.";
	char* ret = NULL;
	for (ret = strtok(copy, sep); ret != NULL; ret = strtok(NULL, sep))
	{
		printf("%s\n",ret);
	}

	/*char* ret = strtok(copy, sep);
	printf("%s\n",ret);

	ret = strtok(NULL, sep);
	printf("%s\n", ret);

	ret = strtok(NULL, sep);
	printf("%s\n", ret);*/

	return 0;
}

strerror

char * strerror ( int errnum );

返回错误码,所对应的错误信息。库函数在执行时,发生了错位会将一个错误码存放在errno这个变量中,errno是C语言提供的一个全局变量

#include <stdio.h>
int main()
{

	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d:%s\n", i, strerror(i));
	}
	return 0;
}
int main()
{
	FILE* pf = fopen("data.txt","r");
	if (pf == NULL)
	{
		//perror("fopen");
		printf("fopen:%s\n",strerror(errno));
	}
	fclose(pf);
	return 0;
}

字符分类函数:isupper(),islower(),isalpha()

字符转换:tolower(),toupper()

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值