CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to append new value at the beginning of string?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
janet



Joined: 17 Feb 2004
Posts: 23

View user's profile Send private message

How to append new value at the beginning of string?
PostPosted: Tue Mar 16, 2004 2:56 am     Reply with quote

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
Ttelmah
Guest







Re: How to append new value at the beginning of string?
PostPosted: Tue Mar 16, 2004 3:20 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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