字符串中需要掌握的函数总结(1)

字符串中必须掌握的函数总结

目录

字符串中必须掌握的函数总结

1. reverse()函数

2. substr()函数

3. tolower()函数

 4. toupper()函数

5. islower()函数

6. isupper()函数

7. isalpha()函数

8. isdigit()函数

9. isalnum()函数


1. reverse()函数(头文件是 <algorithm>)

可以用来反转数组和字符串

【代码如下】

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int arr[6]={1,2,3,4,5,6};
    reverse(arr,arr+6);
    for(int i=0;i<6;i++)
    cout<<arr[i]<<" ";
    cout<<endl;
    //
    string s="ABCDEFG";
    reverse(s.begin(),s.end());
    cout<<s<<endl;
    
}

【运行结果】

【说明】这个函数可用于判断字符串是否为回文字符串,将反转后的字符串与原来字符串对比,若相同,则是。

2. substr()函数(头文件是 <string>)

截取函数,可用来截取字符(串)

一般有两种用法:

substr(pos,len),从指定位置 pos 开始,截取长度为 len 的字符串;

substr(pos),从指定位置 pos 开始,截取到原始字符串末尾。

【代码如下】

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s="abcdefgh";
	string s1=s.substr(1,5);
	//从下标为 1截取 5个字符
	cout<<s1<<endl; 
	
	//从下标为 4截取至字符串尾部 
	cout<<s.substr(4)<<endl;
}

【运行结果】

3. tolower()函数(头文件是 <stdlib.h>)

将字符大写转小写,使用前提是 char 类型的 ‘单个字符’

【注意】tolower()返回值是 int 类型,它返回转换后的字符的 ASCII 值,所以不能直接输出 tolower(ch),可以对它进行强制类型转换一下。

【代码如下】

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
	char ch='A';
	cout<<char(tolower(ch)); 
}

 4. toupper()函数(头文件是 <stdlib.h>)

将字符大写转小写,使用前提是 char 类型的 ‘单个字符’

【注意】同上面 tolower()

【代码如下】

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
	char ch='a';
	cout<<char(toupper(ch)); 
}

5. islower()函数

判断所传字符是否为小写字母,若是,则返回非 0,否则,返回 0

【代码如下】

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
	string s="6a9DheHF8";
	for(int i=0;i<s.length();i++)
	{
		if(islower(s[i]))
		//如果该字符是小写字母,返回非 0 
		cout<<"Y"<<" ";
		else
		cout<<"N"<<" ";
	}
}

【运行结果】

6. isupper()函数

判断所传字符是否为大写字母,若是,则返回非 0,否则,返回 0

【代码如下】

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
	string s="6a9DheHF8";
	for(int i=0;i<s.length();i++)
	{
		if(isupper(s[i]))
		//如果该字符是大写字母,返回非 0 
		cout<<"Y"<<" ";
		else
		cout<<"N"<<" ";
	}
}

【运行结果】

7. isalpha()函数(头文件是 <ctype.h>)

用来判断字符是否为字母,若是,则返回非 0,否则,返回 0

【代码如下】

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
	string s="2h3nbr74";
	for(int i=0;i<s.length();i++)
	{
		if(isalpha(s[i]))
		//如果该字符是字母,返回非 0 
		cout<<"Y"<<" ";
		else
		cout<<"N"<<" ";
	}
}

【运行结果】

8. isdigit()函数(头文件是 <ctype.h>)

用来判断字符是否为(十进制)数字,若是,则返回非 0,否则,返回 0

【代码如下】

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
	string s="2h3nbr74";
	for(int i=0;i<s.length();i++)
	{
		if(isdigit(s[i]))
		//如果该字符是数字,返回非 0 
		cout<<"Y"<<" ";
		else
		cout<<"N"<<" ";
	}
}

【运行结果】

9. isalnum()函数(头文件是 <ctype.h>)

用来判断字符是否为(十进制)数字或者字母,若是,则返回非 0,否则,返回 0

【代码如下】

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
	string s="2h:3n.br?74";
	for(int i=0;i<s.length();i++)
	{
		if(isalnum(s[i]))
		//如果该字符是字母或数字,返回非 0 
		cout<<"Y"<<" ";
		else
		cout<<"N"<<" ";
	}
}

【运行结果】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值