0

I have a character array of some size in C++ program

char current_time[30];

The value of the character array is 2015-02-24T21:39:02.xxx+0800

The "xxx" in the character array must be replaced with a three digit number which is stored in a integer.

How can I replace efficiently without using for loop in C++? The position of the XXX is also known in the character array.

Langdon
  • 53
  • 1
  • 8

1 Answers1

0

If you know the position of XXX in character array as x,y,z, you can update array as follows if your 3 digit number is N.

  int N2=N%100;
  int N3=N%10;

current_time[x]=N/100;
current_time[y]=N2%10;
current_time[z]=N3;
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • I'm looking for an in-built replace function. That replaces from the start position to end position with the given integer numbers. – Langdon Feb 24 '15 at 22:17
  • Use std::string.replace() – Steephen Feb 24 '15 at 22:20
  • When I use string replace, it overwrites xxx with a value I specify but it erases the +0800 from the character array/String. I just want to replace XXX without any damage to other characters. – Langdon Feb 24 '15 at 22:28
  • 1
    It sounds like you might be calling std::string.replace() incorrectly for what you want to do if it's replacing the rest of your string. Please post what command you're using when you see that output – Penguinfrank Feb 24 '15 at 22:39
  • I apologize I got to know how to use String replace. But my problem is I have only character buffer not a string. And converting this character buffer to string and using replace is little bit lengthy. Is there a way to change the XXX characters alone directly from a character buffer ?? – Langdon Feb 24 '15 at 22:44
  • Convert to `string` at the first opportunity. Seriously, it makes everything much easier. – Alan Stokes Feb 24 '15 at 23:04