一、C Cstring函数
C Cstring函数包含了很多字符串处理的函数,比如strcpy、strcat、strlen等,它们是C语言中的传统函数。在C++编程中,我们通常使用C++中的string类来进行字符串的处理。但是在某些情况下,我们需要将string类型转化为char*类型的C字符串类型,这个时候就需要用到c_str()函数。
#include
#include
using namespace std;
int main()
{
string str="hello world!";
const char *cstr=str.c_str();
cout<<cstr<<endl;
return 0;
}
上述代码中,我们将string类型的str转化为const char类型的cstr,然后输出cstr。我们可以看到,输出结果是hello world!。
二、C Cstring类
C Cstring是C++中的一个基础字符串类,它包含了很多操作C字符串的函数,如strlen、strcpy、strcat等。
#include
#include
using namespace std;
int main()
{
char str1[]="hello";
char str2[]="world";
strcat(str1," ");
strcat(str1,str2);
cout<<str1<<endl;
return 0;
}
上述代码中,我们首先定义了两个char类型的字符串,然后用strcat函数将它们拼接起来,并将结果输出。输出结果是hello world。
三、C Cstring循环
在C++中,字符串可以看做是一个字符数组,我们可以用循环来对字符串中的每个字符进行访问和操作。
#include
#include
using namespace std;
int main()
{
string str="hello world";
for(int i=0;i<str.length();i++)
{
cout<<str[i]<<" ";
}
return 0;
}
上述程序中,我们遍历了字符串str中的每个字符,并将它们输出。输出结果是h e l l o w o r l d。
四、C Cstring头文件
C Cstring头文件中包含了很多对字符串进行操作的函数,如strlen、strcpy、strcat等。
#include
#include
using namespace std;
int main()
{
char str1[]="hello";
char str2[]="world";
if(strcmp(str1,str2)==0)
{
cout<<"两个字符串相等"<<endl;
}
else
{
cout<<"两个字符串不相等"<<endl;
}
return 0;
}
上述程序中,我们通过调用strcmp函数比较了两个字符串的大小,并输出结果。
五、C Cstring转const char
在C++中,我们通常使用string类型来进行字符串的操作。但是在一些函数调用中,我们需要将string类型转为const char*类型的C字符串。
#include
#include
using namespace std;
void print(const char* str)
{
cout<<str<<endl;
}
int main()
{
string str="hello";
print(str.c_str());
return 0;
}
上述代码中,我们定义了一个print函数,它的参数是一个const char*类型的C字符串,在main函数中,我们定义了一个string类型的变量str,然后将它转化为C字符串,并将它作为参数传递给print函数,输出结果是hello。
六、C Cstring如何替换某个字符
C Cstring中没有提供替换某个字符的函数,但是我们可以用循环来实现。
#include
using namespace std;
int main()
{
char str[]="hello world";
char oldchar='o';
char newchar='k';
for(int i=0;i<strlen(str);i++)
{
if(str[i]==oldchar)
{
str[i]=newchar;
}
}
cout<<str<<endl;
return 0;
}
上述程序中,我们首先用char数组定义了一个字符串,然后用for循环遍历每个字符,找到需要替换的字符,并将它替换为新字符。输出结果是hkllw wrld。
七、C Cstring字符串分割
在C++中,没有提供字符串分割函数,但是我们可以用循环和strtok函数来实现。
#include
#include
using namespace std;
int main()
{
char str[]="hello,world,!";
char *pch;
pch=strtok(str,",");
while(pch!=NULL)
{
cout<<pch<<" ";
pch=strtok(NULL,",");
}
return 0;
}
上述程序中,我们用strtok函数将字符串str分割成多个子字符串,并将它们输出。输出结果是hello world !。
八、C Cstring转string
在C++中,我们通常使用string类来进行字符串的操作,但是有些函数需要使用C字符串类型,这时我们需要将string类型转化为C字符串类型。
#include
#include
using namespace std;
int main()
{
char cstr[]="hello";
string str(cstr);
cout<<str<<endl;
return 0;
}
上述代码中,我们首先定义了一个C字符串类型的变量cstr,然后用它定义了一个string类型的变量str,并将它输出。输出结果是hello。
九、C Cstring字符串分割到list中
在C++中,我们可以用C++自带的STL容器list来实现字符串的分割。
#include
#include
#include
using namespace std;
int main()
{
string str="hello,world,!";
list strlist;
string sub;
for(int i=0;i0)
{
strlist.push_back(sub);
}
for(list::iterator iter=strlist.begin();iter!=strlist.end();iter++)
{
cout<<*iter<<" ";
}
return 0;
}
上述代码中,我们定义了一个字符串变量str,然后定义了一个list容器strlist,并通过for循环将str分割成多个子字符串,并将它们添加到strlist中。最后,我们用迭代器遍历strlist,将其中的子字符串输出。输出结果是hello world !。
原创文章,作者:ZEQR,如若转载,请注明出处:https://www.506064.com/n/147892.html
微信扫一扫
支付宝扫一扫