C语言字符串总结

不允许改变字符串字面值:

char *p = "hello";
*(p + 1) = 'a';//不要这样个干
printf("%s\n", p);

char a={'a','b','c'};

这里不会自动添加\0

常见字符串操作,先列这几个吧。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
//删除字符串中指定字符
char * DeleteChar(char *s, char c)//c为要删除字符
{
	int i, j = 0;
	for ( i = 0; *(s + i) != '\0'; i++)
	{
		if (*(s + i) != c)
		{
			*(s + j) = *(s + i);
			j++;
		}
	}
	*(s + j) = '\0';
	return s;
}

//求字符串长度
int str_length(const char *s)
{
	int count = 0;
	while (*s++ != '\0')
	{
		count++;
	}
	return count;
}


//字符串大小写转换
void Tolower(char *s)
{
	
	for (int i = 0; *(s + i) != '\0'; i++)
	{
		if (*(s + i) >= 65 && *(s + i) <= 90)
		{
			*(s + i) = *(s + i) + 32;
		}
	}
		
}

//字符串连接
char * str_cat(char *s1, const char *s2)
{
	char *temp = s1;
	//while (*s1++);这里如果s1为空,这样写会有问题。
	while (*s1)
		s1++;
	while (*s1++ = *s2++);
	return temp;
}

char * ctr_ncat(char *s1, const char *s2, int n)
{
	char * temp = s1;
	while (*s1)
		s1++;
	while (n--)
	{
		if (!(*s1++ = *s2++))
			break;
	}
	*s1 = '\0';
	return temp;
}


//字符串复制		
char * str_cpy(char* s1, const char *s2)
{
	char * temp = s1;
	while (*s1++ = *s2++);
	return temp;
}

//str_ncp
char * stc_ncpy(char *s1, const char *s2, int n)
{
	char *temp = s1;
	while (n--)
	{
		if (!(*s1++ == *s2++))
			break;
	}
	while (n--)
		*s1++ = '\0';

}

//字符串比较strcmp,大于返回1,小于返回-1,相等返回0
int str_cmp(const char *s1, const char *s2)
{
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	return *(unsigned char *)s1 > *(unsigned char *)s2 ? 1 : -1;//这里进行类型转换好一点
}

int main(void)
{
	char s[20] = "hefahifAZfpiwf";

	//字符串长度
	printf("字符串长度为%d\n", str_length(s));

	//删除字符串中'h'字符
	DeleteChar(s, 'h');
	printf("%s\n", s);

	//转换为小写字符
	Tolower(s);
	printf("转换为小写字母:%s\n", s);

	//连接两个字符
	char s1[30] = "haha";
	char s2[10] = "ffafhi";
	str_cat(s1, s2);
	printf("将s2连接到s1后:%s\n", s1);

	char s3[10] = "zhaf";
	char s4[10] = "fah";
	str_cpy(s3, s4);
	printf("将s4复制到s3后:%s\n", s3);
}

C语言自带的字符串处理函数

1.strlen,求字符串长度

size_t strlen(const char *s)
{
	size_t len = 0;
	while (*s++)
		len++;
	return len;
}

不包括null字符在内的字符串长度

2.strcpy,字符串复制

char *strcpy(char *s1, const char *s2)
{
	char *temp = s1;
	while (*s1++ = *s2++);
	return temp;
}

strcpy把\0也复制过去了。

3.strlen和sizeof区别

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	
	printf("%d %d\n", sizeof("hello"), strlen("hello"));
	return 0;
}

strlen不包括\0,sizeof包括。

4.注意字符‘0’和转义字符'\0'不一样

strlen("0123456789")的值为8。sizeof的值为11。

有0的时候注意一下8进制和16进制

\ddd八进制,\xhh十六进制

strlen("\0123456789")的长度为8,sizeof的值为9。

5. strstr函数

char *strstr(const char *haystack, const char *needle);

在字符串haystack中查找needle第一次出现的位置,不包含"\0" ;如果找到,返回对应指针,如果没找到,返回NULL。

https://www.runoob.com/cprogramming/c-function-strstr.html 

needle:针

haystack:干草堆

a needle in a haystack :草垛里的针;几乎不可能找到的东西(有点意思)

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

int main(int argc, char const *argv[])
{
	const char haystack[20] = "how are you.";
	const char needle[5] = "are";
	char *pos = NULL;
	pos = strstr(haystack,needle);
	printf("%s\n",pos);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值