C string manipulation

Posted on 2003-12-05
Last Modified: 2010-04-15
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.
Question by:aomega
  • 4
  • 2
LVL 45

Accepted Solution

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
LVL 45

Expert Comment

ID: 9886602
another alternative is to use strtok

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

beware, strtok will modify your original string unlike strchr

Author Comment

ID: 9886791
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.
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

LVL 45

Expert Comment

ID: 9886799
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

Author Comment

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';

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

Expert Comment

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 );

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now