How to replace certain items in a char array with an integer in C++? -
below example code not working way want.
#include <iostream> using namespace std; int main() { char testarray[] = "1 test"; int numreplace = 2; testarray[0] = (int)numreplace; cout<< testarray<<endl; //output "? test" wanted 2, not '?' there //i trying different things , hoping (int) helped testarray[0] = '2'; cout<<testarray<<endl;//"2 test" want, hardcoded in //is there way based on variable? return 0; }
in string characters , integers, how go replacing numbers? , when implementing this, different between doing in c , c++?
if numreplace
in range [0,9] can :-
testarray[0] = numreplace + '0';
if numreplace
outside [0,9] need
- a) convert
numreplace
string equivalent - b) code function replace part of string evaluated in (a)
ref: best way replace part of string in c , other relevant post on so
also, since c++ code, might consider using std::string
, here replacement, number string conversion, etc simpler.
Comments
Post a Comment