如何在一个字符串中插入另一个字符串

kjeny2002 2007-01-17 10:58:51
如:hello world,hello world!
要求在空格的地方插入一个逗号,不知道怎么写

hello, world,hello, world!
...全文
1033 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
HAPPY冰石 2007-01-18
  • 打赏
  • 举报
回复
//Build in C#
public static void main(string[] args)
{
string str = "hello world,hello world!";
string result = str.Replace(" ", ", ");
Console.Out.WriteLine(result);
}
todototry 2007-01-17
  • 打赏
  • 举报
回复
/*******************************************
*
*insert a character into a existed string
*
*******************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * insert(char * pDstString, char * pSrcString, int iPosInString)
{
if (!pDstString)
{
printf("The destination string is not existed!");
return NULL;
}

int iLengthOfDstString = strlen(pDstString);
if (iPosInString > iLengthOfDstString + 1 || iPosInString < 0)
{
printf("The specified position in the string is not valid!");
return NULL;
}

int iLengthOfSrcString = strlen(pSrcString);

char * pResultString = NULL;
pResultString = (char *)malloc((iLengthOfDstString + iLengthOfSrcString + 1) * sizeof(char));
if (pResultString == NULL)
{
return NULL;
}

strcpy(pResultString, pDstString);
strcpy(pResultString + iPosInString, pSrcString);
strcpy(pResultString + iLengthOfSrcString, pDstString + iPosInString);

return pResultString;
}

int main()
{
char * pDst = "abc";
char * pSrc = "okokok";
char * pResult = NULL;
pResult = insert(pDst, pSrc, 1);
printf("%s", pResult);
free(pResult);
return 0;
}
pluton 2007-01-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

char * dotadd(char *str)
{
char s[100];
char *p = s;
while(*str!='\0')
{
if(*(str+1)!=' ')
{
*p++ = *str++;
}
else
{
*p++ = *str++;
*p++ = ',';
}
}
strcpy(str,s);

return str;
}

int main()
{
char str[100] = "hello world,hello world!";
puts(dotadd(str));

return 0;
}
fosjos 2007-01-17
  • 打赏
  • 举报
回复
char a[100] = "hello world,hello world!";
int count = 0, i = 0;
for(;!a[i];i++)
if(a[i] == ' ')
count++;
for(;i>= 0;i--){
a[i+count] = a[i];
if(a[i]==' ')
a[i+(--count)] = ',';
}
laiwusheng 2007-01-17
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

void fun(char * deststr,char * sourstr,int pos);

int main(){
int i=0;
char str1[]="this is a destination string!";
char str2[]="******";
fun(str1,str2,4);
printf("After calling fun,the str1 is \n");
printf("%s\n",str1);
return 0;
}
void fun(char * deststr,char * sourstr,int pos){
int length1;
int length2;
int flag;
char * string1;
char * string2;
char * p;
string1=deststr;
length1=0;
length2=0;
while(*string1++!='\0'){
length1++;
}

string2=sourstr;
while(*string2++!='\0'){
length2++;
}
printf("len1=%d,len2=%d\n",length1,length2);

if(pos>length1){
printf("out of the string range!\n");
return ;
}

p=(char * )malloc(length1-pos);

string1=deststr;
string2=sourstr;
string1=string1+pos;
flag=0;
do{
p[flag++]=*string1++;
}while(*string1!='\0');
p[flag]='\0';

string1=deststr+pos;
for(flag=0;flag<length2;flag++)
*string1++=string2[flag];

flag=0;
while(p[flag]!='\0')
*string1++=p[flag++];
free(p);
}
//其中pos可用泛型算法find插入任意你要插的位置
lockhall 2007-01-17
  • 打赏
  • 举报
回复
Mark....抽空写个看看.
zhuojohn 2007-01-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string insert_symbol(string, string);

void main()
{
string input("hello world,hello world!");
string output;
string symbol(",");

output=insert_symbol(input,symbol);

cout<<"orginal string is: "<<input<<endl;
cout<<"formatted string is: "<<output<<endl;

system("PAUSE");
}

string insert_symbol(string input_string, string symbol_string)
{
string result;

vector<string> buffer;
string::size_type pos=0, prev_pos=0;

while (( pos = input_string.find_first_of( ' ', pos ))!= string::npos )
{
buffer.push_back( input_string.substr(prev_pos, pos-prev_pos));
prev_pos = ++pos;
}

buffer.push_back(input_string.substr( prev_pos, pos - prev_pos ));

vector<string>::iterator iter= buffer.begin();
vector<string>::iterator end_iter= buffer.end();

result.clear();

for(; iter != end_iter; ++iter)
if (iter!= end_iter-1)
result=result + *iter + symbol_string + string(" ");
else
result= result+ *iter;

return result;
}
BtInside 2007-01-17
  • 打赏
  • 举报
回复
为什么不用正则表达式呢?
shell下面就是s/ /,/
kjeny2002 2007-01-17
  • 打赏
  • 举报
回复
todototry

我改了一下你的代码。呵呵
strcpy(pResultString, pDstString);
strcpy(pResultString + iPosInString, pSrcString);
strcpy(pResultString + iPosInString + iLengthOfSrcString, pDstString + iPosInString);
rcom10002 2007-01-17
  • 打赏
  • 举报
回复

char * dotadd(char *str)
{
int size = 0;
do {
// printf("%c\n", str[0]);
if ( str[0] == ' ' )
{
str[0] = ',';
}
// printf("%c\n", str[0]);
++size;
} while (*str++);
return (str - size);
}

int main(int argc, char *argv[])
{
char str[100] = "hello world, hello world!";
puts(dotadd(str));
return EXIT_SUCCESS;
}
todototry 2007-01-17
  • 打赏
  • 举报
回复
比如源串:hello world!
目的串:<font color=red>hello<font> world!
那你就用两次,第一位置5,插入串<font>,第二次位置0,插入串<font color=red>,就行了啊,这次说清楚了吧
todototry 2007-01-17
  • 打赏
  • 举报
回复
你想插到什么位置,那就指出合适的下标啊,我那个0,1只是作个例子
kjeny2002 2007-01-17
  • 打赏
  • 举报
回复
int main()
{
char * pDst = "hello world!";
char * pSrc = "<font color=red>";
char * pResult = NULL;
pResult = insert(pDst, pSrc, 1);
printf("%s\n\n", pResult);
free(pResult);
return 0;
}

[root@server c]# ./insert
h<font color=redello world!
以上肯定不对呀
kjeny2002 2007-01-17
  • 打赏
  • 举报
回复
应该是
aokokokbc
这里少了一个k
aokokobc
kjeny2002 2007-01-17
  • 打赏
  • 举报
回复
一楼的也有问题呀
int main(){
int i=0;
char str1[]="this is a destination string!";
char str2[]="<font color=red>";
char str3[]="<font>";
fun(str1,str3,4);
fun(str1,str2,0);
printf("After calling fun,the str1 is \n");
printf("%s\n\n\n",str1);
return 0;
}

./insert2
After calling fun,the str1 is
<font color=red>this<font> is a destination string!縛栻卡栻?蜧


有乱码呀
todototry 2007-01-17
  • 打赏
  • 举报
回复
怎么说不对呢,位置1(下标从0开始记),不就是在a的后面插入okokok嘛,
下标你可以随便的指定成源串中的任何一个位置
比如pResult = insert(pDst, pSrc, 0);则会okokokabc
kjeny2002 2007-01-17
  • 打赏
  • 举报
回复
楼上的出来aokokobc 好像不对呢

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧