String C++;在字符串中插入字符
我想在字符串的每个数字后面插入一些字符 我找到了如何在每个字符前插入,但我想要的是: 字符串str=“12+5” 在每个数字后插入^ Output=“12^+5^”String C++;在字符串中插入字符,string,insert,String,Insert,我想在字符串的每个数字后面插入一些字符 我找到了如何在每个字符前插入,但我想要的是: 字符串str=“12+5” 在每个数字后插入^ Output=“12^+5^” 感谢您的帮助类似的方法应该会奏效: int main() { char string[128] = "12 + 5"; char output[128] = {}; char side_string[128] = {}; int pos_string = 0; for (pos_string = 0; pos <
感谢您的帮助类似的方法应该会奏效:
int main()
{
char string[128] = "12 + 5";
char output[128] = {};
char side_string[128] = {};
int pos_string = 0;
for (pos_string = 0; pos < length(string); pos_string ++) {
if (string[pos_string] < '0' && string[pos_string] > '9') {
if (strlen(side_string) > 0) {
strcat(output, side_string);
strcat(output, "^");
side_string[0] = 0;
}
output[strlen(output)] = string[pos_string];
} else {
side_string[strlen(side_string)] = string[pos_string];
}
}
if (strlen(side_string) > 0) {
strcat(output, side_string);
strcat(output, "^");
side_string[0] = 0;
}
intmain()
{
字符串[128]=“12+5”;
字符输出[128]={};
char-side_字符串[128]={};
int pos_字符串=0;
for(pos_string=0;pos'9'){
如果(strlen(侧边字符串)>0){
strcat(输出,侧字符串);
strcat(输出,“^”);
边字符串[0]=0;
}
输出[strlen(输出)]=字符串[pos_字符串];
}否则{
侧弦[strlen(侧弦)]=弦[pos_弦];
}
}
如果(strlen(侧边字符串)>0){
strcat(输出,侧字符串);
strcat(输出,“^”);
边字符串[0]=0;
}
注:
提供完整的代码,提供您尝试搜索的内容,如1h30,但我没有找到解决方案。所有主题和文档都是关于在特定位置插入内容。我想在每个数字后添加我的字符。因此,我没有任何代码,抱歉。我知道这就像“为我做”一样,但如果你能给我指路,我将不胜感激