怎么做字符串运算?如:把a("aaaa")变成a("aabbbaa")或者a("aa56a4a")?

Littleming 2005-01-20 10:24:17
大家帮忙
...全文
211 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
CMyMfc 2005-01-20
  • 打赏
  • 举报
回复
那就完全没必要自己定义函数了
直接用sprintf不就行了?

如果非要用的话,就要用可变参数列表
va_list
va_start
va_arg
va_end
Littleming 2005-01-20
  • 打赏
  • 举报
回复
:) 揭贴了,不好意思
winstonch 2005-01-20
  • 打赏
  • 举报
回复
哈哈,这个小伙的要求越来越高了,还是直接用标准函数吧
xuelong_zl 2005-01-20
  • 打赏
  • 举报
回复
这样对分配内存空间的大小,一定要心中有数
Littleming 2005-01-20
  • 打赏
  • 举报
回复
后面的参数要不定个数的
CMyMfc 2005-01-20
  • 打赏
  • 举报
回复
可以, 不过不安全

像你这么传参数是不行的 需传字符串数组 char a[100] = "aaaaaaa";
func(a, 1, 2);
然后a 的内容为"aaaaaaa12";

char *fun(char *a, int b, int c)
{
sprintf(a, "%s%d%d", a, b, c);
return a;
}
Littleming 2005-01-20
  • 打赏
  • 举报
回复
to CMyMfc(星际人生:=E.F=FlyForEver) ( ) 信誉:100
谢谢!
能不能写个函数可以:
func(a,...)
运用:
char *a;
a="aaaaaaa";
a=func(a,1,2);

out a="aaaaaaa12";
winstonch 2005-01-20
  • 打赏
  • 举报
回复
还是不懂,举个例子吧
CMyMfc 2005-01-20
  • 打赏
  • 举报
回复
char *a = "aaa";
int b = 10;
char *c = "bbb";

char str[128];

sprintf(str, "%s%d%s", a, b, c);

//结果str为"aaa10bbb"
Littleming 2005-01-20
  • 打赏
  • 举报
回复
呵呵,不是,是指附值运算,象print("aa%daa"),56
winstonch 2005-01-20
  • 打赏
  • 举报
回复
什么意思?
kobefly 2005-01-20
  • 打赏
  • 举报
回复
变魔术?
xuwedo2003 2005-01-20
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include "stdafx.h"
#include <string>

using namespace std;

char buffer [65];

string& fun(string& source,int a,int b)
{
string s_a(_itoa(a,buffer,10));
string s_b(_itoa(b,buffer,10));

return source=source + s_a + s_b;
}
int _tmain(){
string sour("aaaaaaaa");
cout<<fun(sour,1111,22222)<<endl;

system("pause");
return 0;
}


aaaaaaaa111122222
请按任意键继续. . .
xuwedo2003 2005-01-20
  • 打赏
  • 举报
回复
#
#include <string>
#include <ios>
using namespace std;



string str("aaaaaaa");

string func(string source,int a ,int b ){
char * pa = new char[source.length()+2];
streamsize i = (cout<<source<<a<<b).rdbuf()->sgetn(pa, 1 );
pa[i] = '\0';


string d(pa);
delete pa;
return d;
}


int _tmain()
{

cout<<func(str,44444441,4444442)<<endl;

system("pause");
return 0;
}
输出:

aaaaaaa444444414444442
请按任意键继续. . .

69,371

社区成员

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

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