Solved

C string manipulation

Posted on 2003-12-05
6
714 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
  • 4
  • 2
6 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now