首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么在一个字符串中插入另一个字符串

2012-03-01 
如何在一个字符串中插入另一个字符串如:helloworld,helloworld!要求在空格的地方插入一个逗号,不知道怎么

如何在一个字符串中插入另一个字符串
如:hello   world,hello   world!
要求在空格的地方插入一个逗号,不知道怎么写

hello,   world,hello,   world!


[解决办法]
#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插入任意你要插的位置
[解决办法]
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)] = ', ';
}

[解决办法]
#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;
}
[解决办法]
/*******************************************
*
*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;
}
[解决办法]

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;
}
[解决办法]
#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;
}
[解决办法]
//Build in C#
public static void main(string[] args)
{
string str = "hello world,hello world! ";
string result = str.Replace( " ", ", ");
Console.Out.WriteLine(result);
}

热点排行