Solved

C string manipulation

Posted on 2003-12-05
6
720 Views
Last Modified: 2010-04-15
Hi,
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.
Thanks.
0
Comment
Question by:aomega
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9886598
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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9886602
another alternative is to use strtok

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

beware, strtok will modify your original string unlike strchr
0
 

Author Comment

by:aomega
ID: 9886791
SunnyCoder,
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9886799
it is not subtracting arrays but pointer .. pointers are nothing but addresses (numbers)
e.g.

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
0
 

Author Comment

by:aomega
ID: 9886843
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';
return(buffer);
}

I am gettting a segfault at the line strncpy.  I am not quite sure why that is in this case.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9886861
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 );
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question