循环以在字符串中继续添加空格? [英] Loop to keep adding spaces in string?
问题描述
我有以下代码:
sHexPic = string_to_hex(sPic);
sHexPic.insert(sHexPic.begin()+ 2,’’);
sHexPic.insert(2,);
我想知道如何将其放入计数循环并在每2nd之后添加一个空格字符。到目前为止,所有这些操作都是将字符串 35498700变成 35 498700,最后我希望最终结果是类似 35 49 87 00的东西。
我假设您必须获取字符串的长度和字符串中的字符数。
我正在尝试在c ++ / cli中实现这一点。 / p>
谢谢。
这是在C ++中的实现方式,使用字符串:)(我正在使用C库,因为我对C更熟悉)
#include< stdio .h>
#include< string>
使用命名空间std;
int main()
(
字符串X;
int i;
int y;
X = 35498700;
y = X.size();
for(i = 2; i {
X.insert(i, );
y = x.size(); //更新x
i ++的大小; //跳过插入的空间
}
printf(% s,X);
返回0;
}
玩得开心:)
这可能起作用。如果没有,请提一下:)
I have the following code:
sHexPic = string_to_hex(sPic);
sHexPic.insert(sHexPic.begin() + 2,' ');
sHexPic.insert(2," ");
I would like to know how I can put this into a counted loop and add a space after every 2nd character. So far all this does is make this string "35498700" into "35 498700", which in the end I want the final result to be something like "35 49 87 00".
I assume you would have to get the length of the string and the amount of characters in it.
I am trying to achieve this in c++/cli.
Thanks.
Here's how it would be done in C++, using a string :) (I'm using C libraries cuz I'm more familiar with C)
#include <stdio.h>
#include <string>
using namespace std;
int main()
(
string X;
int i;
int y;
X = 35498700;
y= X.size();
for(i=2;i<y;i+=2)
{
X.insert(i," ");
y=x.size(); //To update size of x
i++; //To skip the inserted space
}
printf("%s",X);
return 0;
}
Have fun :)
That would "probably" work. If it didn't then please mention so :)
这篇关于循环以在字符串中继续添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!