![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
janet
Joined: 17 Feb 2004 Posts: 23
|
How to append new value at the beginning of string? |
Posted: Tue Mar 16, 2004 2:56 am |
|
|
let's say
Code: |
BYTE random_value;
BYTE random_string[10];
BYTE data_string[] = "";
for (i = 0; i < 10; i++) {
random_value = rand();
sprintf(random_string, "%u", random_value);
strcat(data_string, random_string);
next;
|
every new data will be appended at the end of the string.
the string will contain "random_value0 random_value1 random_value2 ..."
What if i want to reverse the order of value in the string so that new value will be appended at the BEGINNING of the string?
the end result i want to get is "random_value9 random_value8 random_value7 ... random_value0"
Cheers |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: How to append new value at the beginning of string? |
Posted: Tue Mar 16, 2004 3:20 am |
|
|
janet wrote: | let's say
Code: |
BYTE random_value;
BYTE random_string[10];
BYTE data_string[] = "";
for (i = 0; i < 10; i++) {
random_value = rand();
sprintf(random_string, "%u", random_value);
next;
|
every new data will be appended at the end of the string.
the string will contain "random_value0 random_value1 random_value2 ..." strcat(data_string, random_string);
What if i want to reverse the order of value in the string so that new value will be appended at the BEGINNING of the string?
the end result i want to get is "random_value9 random_value8 random_value7 ... random_value0"
Cheers |
It'd be better (in terms of speed), to think again, and perhaps store the strings 'backwards'. You can append at the beginning, by doing:
strcat(random_string, data_string);
strcpy(data_string, random_string);
However you do it though, if you actually need to end up with th data stored this way, there will be a lot of data movement involved, since you have to shift the entire string in memory to make the space.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|