C++强大的字符串处理函数和C指针处理字符串的对比

实现字符串的切割和重组。
实现字符串的8位切割,不够8位补0实现。
输入:
1a2s3d4f5g6h7j8k9l0z1x2c3v4b5n6m7a8s9d 123 1111111111111111111111111111111111111111111111111111 q
1
输出:
1a2s3d4f 5g6h7j8k 9l0z1x2c 3v4b5n6m 7a8s9d00 12300000 11111111 11111111 11111111 11111111 11111111 11111111 11110000 q0000000 10000000

#include
#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <memory.h>
#include
using namespace std;
int main()
{
string str[5];
string str_temp;
int i=0,j=0;
for(i = 0;i<5;i++ )
{cin>>str[i];}
for(i = 0;i<5;i++ )
{
int str_len = str[i].size();
if(str_len < 8) {
//str[i].append(str_temp1,(8-str_len));
str_temp = str[i].insert(str_len,8-str_len,‘0’);
str_temp.append(" “);
//sort(str_temp.begin(), str_temp.end());
cout<<str_temp;
str_temp.clear();}
else
{
for(j=0 ;j < str_len / 8;j++)
{
//str_temp = str[i].insert(8j,1,’ ');
str_temp.assign(str[i],8
j,(8*j)+8);
str_temp.assign(str_temp,0,8);
str_temp.append(” “);
//sort(str_temp.begin(), str_temp.end());
cout<<str_temp;
str_temp.clear();
}
int str_y = str_len%8;
if(str_y > 0)
{
//str_temp = str[i].insert(str_len,(8 - str_len % 8),‘0’);
str_temp.assign(str[i],str_len-str_y,str_len);
str_temp = str_temp.insert(str_y,8-str_y,‘0’);
str_temp.append(” ");
cout<<str_temp;
str_temp.clear();
}
}
}
return 0;
}

C++强大的字符串处理功能,是的C++可以灵活的实现字符串的各种操作,快捷方便。C++字符串处理函数介绍:https://www.cnblogs.com/Draymonder/p/6944479.html

对比使用C指针的方式实现字符串的切割重组。
#include
#include <string.h>
#include <stdio.h>
#include <memory.h>
#include
using namespace std;
int main()
{
char a[10][100];
for(int i = 0;i<5;i++ )
{cin>>a[i];}
char str_temp[9] = {‘0’,‘0’,‘0’,‘0’,‘0’,‘0’,‘0’,‘0’};
string str_temp1 = “00000000”;
for(int i = 0;i<5;i++ )
{
int str_len = strlen(a[i]);
char p_str;
p_str = a[i];
str_temp1.clear();
str_temp1 = “00000000”;
if(str_len < 8) {
strncpy(str_temp,a[i],(str_len));
// memcpy(str_temp,a[i],(str_len));
str_temp1 = str_temp;
sort(str_temp1.begin(), str_temp1.end());
cout<<str_temp1<<" "; }
else
{
int str_count = str_len / 8 ;
int str_yu = str_len % 8;
for(int j=0 ;j < str_count ;j++)
{
strncpy(str_temp,p_str+8
j,8);
// memcpy(str_temp,p_str+8j,8);
str_temp1 = str_temp;
sort(str_temp1.begin(), str_temp1.end());
cout<<str_temp1<<" ";
}
for(int j=0 ;j < 8 ;j++)
{str_temp[j] = ‘0’;}
str_temp1.clear();
str_temp1 = “00000000”;
strncpy(str_temp,p_str+8
str_count,str_yu);
//memcpy(str_temp,p_str+8*str_count,str_yu);
str_temp1 = str_temp;
sort(str_temp1.begin(), str_temp1.end());
cout<<str_temp1<<" ";
}
for(int j=0 ;j < 8 ;j++)
{str_temp[j] = ‘0’;}
}
return 0;
}

在C++里面无法通过指针操作string类型的字符串。所以对于这种字符串的处理最好的方式就是采用二维数组的方式,然后配合指针来实现字符串的灵活操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值