LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-25-2005, 03:02 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,087

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
prepend char to a string


i have this array:
char* col1[500].
i want to put a char in front of it.

i guess the easiest way is to use strcat(). what's the quickest way to reverse the array? so i could stick a '0' at the end of it and reverse it back?
i'm doing this in c.

thanks,
 
Old 05-25-2005, 03:19 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Some C libraries have strrev().

This reverses a char array
Code:
char *str_rev(char *s){
    char *p=s;
    char *q =s;
    char swap;
    if (*s)
    {
        q=strchr(q,'\0');
        while (--q > p)
        {
            swap = *q;
            *q = *p;
            *p = swap;
            ++p;
        }
    }
    return s;
}
 
Old 05-25-2005, 03:20 PM   #3
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
You know that ahead of time?

Obviously you could just start building it at [1], but you
probably know that (i.e. if you're getting the array from
somewhere else):
Code:
char final[501];
final[0]= '0';
myFunc( &final[1] );
Quickest way to reverse is to just start at last index and
decrement your count. If you want a completely new
array that is reversed, just allocate another and start
from end and work your way to the beginning.

Allocating another 512 bytes isn't a big deal is it?

EDIT: fixed error in code

Last edited by towlie; 05-25-2005 at 03:24 PM.
 
Old 05-25-2005, 03:31 PM   #4
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,087

Original Poster
Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
thanks guys i'll work on it. unfortunately my aix build doesn't have strrev (thanks for posting it).
i'll start working on your suggestions.
 
Old 05-29-2005, 08:12 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You could use memmove(3) to shift the array more efficiently.
 
Old 05-30-2005, 12:25 PM   #6
kryptonite0110
Member
 
Registered: Mar 2005
Posts: 59

Rep: Reputation: 15
wouldn't it be quicker just to shift the entire string to the right on byte and then sticking a char at the front? eg:

void put_front(char *str, char in)
{
for (size_t i = strlen(str) - 1; i != 1; i--)
{
str[i] = str[i - 1];
}
str[0] = in;
}

just a thought...
 
Old 06-01-2005, 08:53 AM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by kryptonite0110
[B]wouldn't it be quicker just to shift the entire string to the right on byte and then sticking a char at the front? eg:
That's what I was (implicitly) suggesting in my memmove() post above, move the array and load the character at the start.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
how to covert int to string/char type? nelnel Programming 2 08-23-2005 11:46 PM
c++ : replacing a char in a string cb951303 Programming 17 06-06-2005 05:48 PM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM


All times are GMT -5. The time now is 04:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration