• C

C string manipulation

I am trying to extract a substring from a C string(character array).  Specifically, I am trying to extract the substring up to the first delimiter '|'.
Example: if i have the string 'abcd| efg'
I want to get the C string 'abcd' and return it.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

char * string = "abcd| efg";
char buffer[10];

temp = strchr ( string, '|' );
strncpy ( buffer, string, temp - string );
buffer[temp - string] = 0;

now buffer has the required string

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
another alternative is to use strtok

temp = strtok ( string, "|" );
return temp;

beware, strtok will modify your original string unlike strchr
aomegaAuthor Commented:
The 5th line : strncpy ( buffer, string, temp - string ) has temp-string as the third parameter.  I am not familiar with subtracting character arrays. What exactly does this accomplish?  thanks.
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

it is not subtracting arrays but pointer .. pointers are nothing but addresses (numbers)

string starts at 1000

a  1000
b  1001
c   1002
d  1003
|   1004
e  1005
f   1006
g  1007

after strchr string = 1000 and temp = 1004
1004 - 1000 = 4 .. so it will copy 4 chars
aomegaAuthor Commented:
thanks for the explanation.
I have pretty much the same as your first example except char *string being passed as a parameter to the function.

char *getKey(char *line){
static char buffer[1000];
char *temp;

temp = strchr(line, '|');
strncpy(buffer, line, temp-line);
buffer[temp-line] = '\0';

I am gettting a segfault at the line strncpy.  I am not quite sure why that is in this case.
runs perfectly
... the segfault may be at some other line ... also you are not performing any error checking ... what if strchr returns NULL ?

#include <stdio.h>
#include <string.h>

char * getkey ( char * a )
        static char buffer[100];

        char * temp;

        temp = strchr (a, '|' );

        strncpy ( buffer, a, temp - a );

        buffer [ temp - a ] = 0;

        return buffer;

int main ()
        char * a = "abcd|efg";

        char * t;

        t = getkey ( a );

        printf ( "%s\n", t );
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.