【发布时间】:2014-04-06 18:35:32
【问题描述】:
下面是一个示例代码,它没有按我想要的方式工作。
#include <iostream>
using namespace std;
int main()
{
char testArray[] = "1 test";
int numReplace = 2;
testArray[0] = (int)numReplace;
cout<< testArray<<endl; //output is "? test" I wanted it 2, not a '?' there
//I was trying different things and hoping (int) helped
testArray[0] = '2';
cout<<testArray<<endl;//"2 test" which is what I want, but it was hardcoded in
//Is there a way to do it based on a variable?
return 0;
}
在包含字符和整数的字符串中,如何替换数字?在实现这一点时,在 C 和 C++ 中执行它有什么不同?
【问题讨论】:
-
字符常量
'2'的整数值和整数常量2不一样。