UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Extract a portion of a string

#1 2008-06-11 07:36 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

How can i extract a particular portion of a string?
Its like i have a char* buffer and i need to extract some portion of it as in
"Have a nice day.Let Truth prevail"
i wanna extract only "nice day" portion of it.
i tried but i ended up in modifying the original string which i never intend to..
if anyone has the perfect code/macro for this task...
please reply me back with it.
thanks in advance...

Offline

#2 2008-06-11 08:46 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

It depends on which portion of the string you want to extract, and how you're
going to use the extracted part (or what you exactly mean with "extract").
Does the extracted part need to be NUL terminated? If so, you either need to
copy it to its own buffer (e.g. with strndup) or modify the original string.

Offline

#3 2008-06-11 08:51 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

Offline

#4 2008-06-11 09:32 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

So you've the start of the substring and the end?

E.g. start is "nice" and end is "day", then

res = extract("Have a nice, cheerful day people.")

would give res = "nice, cheerful day"?

This is a matter of finding the start and end substrings with e.g. strstr(),
and then copying over the chunk. It becomes tricky when the end is before
the start, or when there are multiple occurances of star or end pieces.

Offline

#5 2008-06-11 09:41 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

Offline

#6 2008-06-11 10:07 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

So you're extracting binary bits, from strings?! Sure you don't mean bytes?
If it's bytes then its very easy to do, but if you really want to extract arbitrary bits
then it's slightly more work. It wouldn't make much sense though.

Offline

#7 2008-06-11 10:13 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

Offline

#8 2008-06-11 10:39 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

Well, with current hardware and programming languages the memory space is byte
addressable, so bytes are the smallest memory level you can work on. If you want to
do anything with bits you need to do it on a byte level or higher.

Working on a bit level in general implies that you're working with binary data, not
NUL terminated strings. Working on a bit level with strings seems slightly bizarre,
I'm very curious what you're trying to do, and why.

Extracting bytes is easy, because it's simple pointer arithmetic to get the substring.

Example:

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char* extract(const char* src, int start, int end)
{
	return strndup(src + start, end - start);
}

int main(void)
{
	const char teststr[] = "It is a wonderful day";

	char* substr = extract(teststr, 4, 10);

	if (substr){
		printf("substr = %s\n", substr);
		// will print "s a wo"
	} else {
		perror("extract failed");
	}
	free(substr);
	return 0;
}

Changing this so that it works with bits isn't that hard, but you need to shift
the bytes as needed and pad the result with zero bits to make it a multiple of
8 bits.

Offline

#9 2008-06-12 12:29 PM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

Offline

#10 2008-06-19 01:41 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char* extract(const char* src, int start, int end)
{
	return strndup(src + start, end - start);
}

int main(void)
{
	const char teststr[] = "It is a wonderful day";

	char* substr = extract(teststr, 4, 10);

	if (substr){
		printf("substr = %s\n", substr);
		// will print "s a wo"
	} else {
		perror("extract failed");
	}
	free(substr);
	return 0;
}

Offline

#11 2008-06-19 12:57 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

Offline

#12 2008-06-19 01:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Extract a portion of a string

My guess: he removed the #include <string.h> or the #define _GNU_SOURCE, so
he no longer has a prototype for strndup(), so the compiler thinks it returns int,
hence the complaint about casting that int to pointer in the return()...  He should also
get a warning about the missing prototype if compiling with -Wall...

Offline

#13 2008-06-19 01:51 PM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

Offline

#14 2008-07-04 05:08 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: Extract a portion of a string

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char* extract(const char* src, int start, int end)
{
	return strndup(src + start, end - start);
}

int main(void)
{
	const char teststr[] = "It is a wonderful day";

	char* substr = extract(teststr, 4, 10);

	if (substr){
		printf("substr = %s\n", substr);
		// will print "s a wo"
	} else {
		perror("extract failed");
	}
	free(substr);
	return 0;
}

Offline

#15 2008-07-04 11:59 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Extract a portion of a string

strndup() works on strings, and binary data can have '\0' anywhere, which sort of
breaks it.

Better to replace strndup with a malloc and a memcpy then, or let the caller do the
memory management.

Offline

#16 2008-08-16 08:34 AM

bodanapu
Guest

Re: Extract a portion of a string

Hi you can use strncpy() function whose syntax is as follows

char * strncpy ( char * destination, const char * source, size_t num );

Change the "source" to "source+start_byte" where start_byte refers to the starting byte of the portion of the string you want to extract.

  • Index
  • » C
  • » Extract a portion of a string

Board footer

Powered by FluxBB