c++字符串处理函数及其用法

本文详细介绍了C语言中常用的字符串操作函数,包括复制、连接、比较等,并通过具体示例展示了如何使用这些函数来处理字符串数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、strcpy(字符串复制)

使用范例:

(把s2的内容复制给s1)

#include<iostream> 
#include<string>
using namespace std; 
int main()
{
char s1[]="abc";
char s2[]="ab";
strcpy(s1,s2);
cout<<s1<<endl;   //s1="ab"
cout<<s2<<endl;   //s2="ab"
return 0;
}


2、strncpy(字符串部分复制)

(把s2的前n个字符覆盖s1的前n个字符)

使用范例

#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[]="bb";
char b[]="abc";
strncpy(a,b,2);
cout<<a<<endl;     //a="ab"
cout<<b<<endl;     //b="abc"
return 0;
}


3、strcat(字符串连接)

(把s2中内容连接到s1中,s2保持不变)

使用范例

#include<iostream>
#include<string>
using namespace std;
int main()
{
char s1[]="ab";
char s2[]="cde";
strcat(s1,s2);
cout<<s1<<endl;    //s1="abcde"
cout<<s2<<endl;    //s2="cde"
return 0;
}


4、strncat(将特定数量字符串连接到另一字符串)

(将s2中前n个字符连接到s1中)

使用范例

#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[]="ab";
char b[]="cdef";
strncat(a,b,3);
cout<<a<<endl;    //a="abcde"
cout<<b<<endl;    //b="cdef"
return 0;
}

5、strcspn(在给定字符串中搜寻某个指定字符第一次出现的位置)

搜索s.r中第一个相同的字符,并返回这个字符在s中的位置

使用范例

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
	char *s="Golden ok";
	char *r="end";
        char *h="uu";
	int n,d;
	n=strcspn(s,r);  
	d=strcspn(s,h);
	cout<<n<<endl;   //n=3
	printf("The first char both in s1 and s2 is: %c\n",s[n]);   //s[n]=d
	cout<<d<<endl;                                              //d=9=strlen(s)
	printf("The first char both in s1 and s2 is: %c",s[d]);     //无输出
	getchar();
	return 0;
}

6、strrchr

查找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回从字符串中的这个位置起,一直到字符串结束的所有字符。如果未能找到指定字符,那么函数将返回NULL。

#include <string.h>
#include <stdio.h>
int main(void)
{
	char string[20];
	char *ptr, c = 'e';
	strcpy(string, "There are two rings");
	ptr = strrchr(string, c);
	if (ptr)
		printf("The character %c is at position: %s\n", c, ptr);    //The character e is at position:e are two rings
	else
		printf("The character was not found\n");                    //没找到
	return 0;
}


7、strpbrk

依次检验字符串s1中的字符,当被检验字符在字符串s2中也包含时,则停止检验,并返回该字符位置,空字符NULL不包括在内。

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
main()
{
	char *s1="Weicome To Beijing";
	char *s2="BiT";
	char *p;
	p=strpbrk(s1,s2);
	if(p)
		printf("%s\n",p);     // 输出:icome To Beijing
	else
		printf("Not Found!\n");
	p=strpbrk(s1, "Da");
	if(p)
		printf("%s",p);
	else
		printf("Not Found!");     //输出:Not Found!
	return 0;
}


8、strstr

在字符串s中寻找与l相匹配的子字符串,

#include<stdio.h>
#include <string.h>
main()
{
	char *s="Golden Golobal View";
	char *l="ol";
	char *p;
	p=strstr(s,l);
	if(p)
		printf("%s\n",p);    //输出:olden Golobal View
	else
		printf("Not Found!\n");
	return 0;
}


9、strcmp (字符串比较大小)

s>l输出1,s=l输出0, s<l输出-1

#include<stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
int main()
{
	char *s="ac";
	char *l="ab";
	int k=strcmp(s,l);
        cout<<k<<endl;      //此种情况下k=1;
	return 0;
}




10、_strdup

strdup()在内部调用了malloc()为变量分配内存,不需要使用返回的字符串时,需要用free()释放相应的内存空间,否则会造成内存泄漏

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
	char *s="Golden Global View";
	char *d;
	d=_strdup(s);
	printf("%s",d);   //Golden Global View
	free(d);
	getchar();
	return 0;
}


11、strchr (查找字符串s中首次出现字符c的位置)

#include <string.h>
#include <stdio.h>
int main(void)
{
	char string[17];
	char *ptr, c = 'r';
	strcpy(string, "This is a string");
	ptr = strchr(string, c);
	if (ptr)
		printf("The character %c is at position: %d\n", c, ptr-string);    //ptr-string=12
	else
		printf("The character was not found\n");
	return 0;
}


12、_strnset(将给定字符串中按指定数目将前n个字符置换为指定字符)

#include <stdio.h>
#include <string.h>
int main(void)
{
	char string[55] = "Tsinghua";
	char letter = 'x';
	printf("string before strnset: %s\n", string);    //string="Tsinghua"
	strnset(string, letter, 3);
	printf("string after  strnset: %s\n", string);    //string="xxxnghua"
	return 0;
}















                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值