Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Incorrect String Split And Concat

string

  • Please log in to reply
4 replies to this topic

#1 alirezan

alirezan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 74 posts

Posted 26 April 2012 - 07:22 AM

Hi

I am trying to concatenate multiple strings but the result is incorrect. Here is what I have:

String: 120425,192612

This is date, time (i.e. 2012-04-25 19:26:12). I would like to attach "20" in front of the date to make it a whole 2012 and remove seconds from the end of the time and concat both strings to get something like this:

201204251926

Finally, I would like to put keyword "date" in front of it and run it with "system" command. I have the following code that kinda works but I get a weird character at the end:

  string str = "3784491,-12229083,120425,194649";
  string dt = str.substr (18,6);
  string tm = str.substr (25, 6);
  char d[13];

  strcat (d, "20");
  strcat (d, dt.c_str());
  strncat (d, tm.c_str(), sizeof (tm));
  cout << d << endl;
  d[13]='\0';

  char a[6] = "date ";
  a[6] = '\0';
  cout << a << endl;

  char po[18];
  strcat (po, a);
  strcat (po, d);
  po[18] = '\0';
  cout << po << endl;


This is the output I get:

201204251946
date
(date 201204251946


I don't understand where the "(" comes from?! Am I missing something?

Any help would be appreciated
Thanks
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 26 April 2012 - 08:09 AM

char d[13];
...
d[13] = '\0';

This is a mistake. When declaring an array of size 13, there won't be an index # 13. The last index is 12. (0-12) 13 is outside the bounds of the array and will result in a segment fault. (crash)

char a[6] = "date ";
  a[6] = '\0';

Same mistake here.

There's no need to explicitly place the null character at the end like this. Null characters are automatically appended by inline declarations and strcat()/strncat() calls.

Try fixing all of these things first as what you're doing could have weird behaviors.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 26 April 2012 - 09:37 AM

String: 120425,192612

This is date, time (i.e. 2012-04-25 19:26:12). I would like to attach "20" in front of the date to make it a whole 2012 and remove seconds from the end of the time and concat both strings to get something like this:


Any reason for not using sprintf ( formated print to a string ) ?
like this :

   sprintf( &result[0], "20%6s", &source[0] );

That little piece will print into result char[] variable the following formated string :
- 20 (fixed)
- 6 first characters of source char[] variable, take into account that since source is a pointer, you can point the start of it to any index of the char[]

Click here for a sprintf explanation page

Its a very powerful instruction and the one i used the most since with that you don't need to strcat, strcpy, anymore, you can also check memcpy for byte moving purposes .

i suggest to check memcpy cause it is null independent ;)
here a memcpy explanation page
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 27 April 2012 - 02:58 AM


std::string date("120425,192612");
std::string full_date("date 20" + date.substr(0, date.size() - 2));
C++ has overloaded operator + for concatenating strings.
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 mnirahd

mnirahd

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 328 posts

Posted 27 April 2012 - 11:55 AM

Hi,

whenever you define a character array, it might have some garbage inside it. so its always good to memset it before you do strcat over it. otherwise result might be different than you expect. For example


char myArray[10];

memset(myArray, 0x00, 10); //clears out the string upto 10 characters with NULL char
strcat(myArray, "Now this be OK);

Now with regard to your problem, can you try to clear the array as I suggested: that is


  char po[18];
  memset(po, 0x00, 18); // clear out

  strcat (po, a);
  strcat (po, d);


Hope this helps!

-Munir
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download