Sub strings

P

Polomint

If I have a string which holds 'Hello World', how would I extract the
'World' part into another string? Is there a sub string function in C?

Thanks in advance...

Polo
 
J

Jordan Abel

If I have a string which holds 'Hello World', how would I extract the
'World' part into another string? Is there a sub string function in C?

Not as such. In this _particular_ case, strchr(str,'W') will get you
what you want, but that's obviously not a general solution.

<ot>
I wrote a function that interacts well with the posix regex API to do
substitutions or copy substrings.
</ot>
 
V

Vladimir S. Oka

Polomint opined:
If I have a string which holds 'Hello World', how would I
extract the 'World' part into another string? Is there a sub
string function in C?

Look for function `strstr()`. It's defined in <string.h>. Here's
a brief description, straight from the Standard (I had it at
hand):

7.21.5.7 The strstr function

Synopsis
#include <string.h>
char *strstr(const char *s1, const char *s2);

Description
The strstr function locates the first occurrence in the string
pointed to by s1 of the sequence of characters (excluding the
terminating null character) in the string pointed to by s2.

Returns
The strstr function returns a pointer to the located string,
or a null pointer if the string is not found. If s2 points to
a string with zero length, the function returns s1.
 
V

Vladimir S. Oka

Vladimir S. Oka opined:
Polomint opined:


Look for function `strstr()`. It's defined in <string.h>. Here's
a brief description, straight from the Standard (I had it at
hand):

Forgot to say: it won't cut the string for you, but will give you the
whereabouts of your substring. It should then be simple to extract it.
 
P

Polomint

Thanks for that info.




Jordan Abel said:
Not as such. In this _particular_ case, strchr(str,'W') will get you
what you want, but that's obviously not a general solution.

<ot>
I wrote a function that interacts well with the posix regex API to do
substitutions or copy substrings.
</ot>
 
P

Polomint

Thank you.



Vladimir S. Oka said:
Vladimir S. Oka opined:


Forgot to say: it won't cut the string for you, but will give you the
whereabouts of your substring. It should then be simple to extract it.

--
BR, Vladimir

If God lived on Earth, people would knock out all His windows.
-- Yiddish saying
 
C

CBFalconer

Polomint said:
If I have a string which holds 'Hello World', how would I extract
the 'World' part into another string? Is there a sub string
function in C?

No. Untested code follows:

#include <string.h>
#include <stdlib.h>

/* get and copy the substring in orig beginning with head */
/* Assigns storage, so the returned value needs to be freed */
/* Returns NULL for no such string, or no memory available */
char *getsubstring(const char *orig, const char *head)
{
char *p, *new = NULL;

if (p = strstr(orig, head))
if (new = malloc(1 + strlen(p))) strcpy(new, p);
return new;
}

If you just need the string specified by head, you already have it,
so no need to do anything. If you want something else, such as
deleting a portion of the original string, you failed to define the
problem properly.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Richard G. Riley

If I have a string which holds 'Hello World', how would I extract the
'World' part into another string? Is there a sub string function in C?

Thanks in advance...

Polo

Finding a substring is in the library : use strstr(). Also see strcasestr().

The rest is not necessarily clear. You wish to extract the string? By
this maybe "cut" the string out of a null terminated character buffer?

So what you might want to then do is to "strcpy" the rest of the
source string over the start of the string you just located. Below is
untested but you will get the idea:

char * refFound = strstr(refBuffer,refStringToLocate);

if(refFound){
/* assumes null terminated character buffer)
strcpy(refFound, refFound+strlen(refStringToLocate));
}

good luck
 
P

Polomint

That's perfect, thanks...


Richard G. Riley said:
Finding a substring is in the library : use strstr(). Also see
strcasestr().

The rest is not necessarily clear. You wish to extract the string? By
this maybe "cut" the string out of a null terminated character buffer?

So what you might want to then do is to "strcpy" the rest of the
source string over the start of the string you just located. Below is
untested but you will get the idea:

char * refFound = strstr(refBuffer,refStringToLocate);

if(refFound){
/* assumes null terminated character buffer)
strcpy(refFound, refFound+strlen(refStringToLocate));
}

good luck
 
M

Mark McIntyre

Finding a substring is in the library : use strstr(). Also see strcasestr().

but be aware that strcasestr() is a nonstandard function, so it may
not exist on your platform.
Mark McIntyre
 
E

Eric Sosman

Richard said:
Finding a substring is in the library : use strstr(). Also see strcasestr().

The rest is not necessarily clear. You wish to extract the string? By
this maybe "cut" the string out of a null terminated character buffer?

So what you might want to then do is to "strcpy" the rest of the
source string over the start of the string you just located. Below is
untested but you will get the idea:

char * refFound = strstr(refBuffer,refStringToLocate);

if(refFound){
/* assumes null terminated character buffer)
strcpy(refFound, refFound+strlen(refStringToLocate));
}

Do NOT use strcpy() if the source and destination might
overlap, as they might in this situation. If refBuffer holds
"Now is the time for all good parties to come to the aid of Man"
all is well if refStringToLocate is "come" but all is NOT well
if refStringToLocate is "time".
 
R

Richard G. Riley

Do NOT use strcpy() if the source and destination might
overlap, as they might in this situation. If refBuffer holds
"Now is the time for all good parties to come to the aid of Man"
all is well if refStringToLocate is "come" but all is NOT well
if refStringToLocate is "time".

refStringToLocate is obviously not in the buffer : otherwise there
would be no need to locate it. But to clarify I am assuming that
refStringToLocate is a seperate buffer.

Am I missing something here? Admittedly I am assuming the strcpy is the
equivalent of "while(*dest++=*src++)"; I'm looking long and hard at
your warning but just dont see how it applies in this context. e.g
compilable and probably breaking some rules for modifying command line
arguments:


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

int main(int argc, char **argv){

if(argc!=3){
printf("exe buffer substring\n");
exit(EXIT_FAILURE);
}

char * refBuffer = argv[1];
char * refStringToLocate = argv[2];

char * refFound = strstr(refBuffer,refStringToLocate);
if(refFound){
/* assumes null terminated character buffer) */
strcpy(refFound, refFound+strlen(refStringToLocate));
}
printf("%s\n",refBuffer);
exit(EXIT_SUCCESS);
}
 
B

Barry Schwarz

If I have a string which holds 'Hello World', how would I extract the
'World' part into another string? Is there a sub string function in C?
Look up strcpy and strncpy in your reference.


Remove del for email
 
E

Eric Sosman

Richard said:
Richard G. Riley wrote:
[...]
So what you might want to then do is to "strcpy" the rest of the
source string over the start of the string you just located. Below is
untested but you will get the idea:

char * refFound = strstr(refBuffer,refStringToLocate);

if(refFound){
/* assumes null terminated character buffer)
strcpy(refFound, refFound+strlen(refStringToLocate));
}

Do NOT use strcpy() if the source and destination might
overlap, as they might in this situation. If refBuffer holds
"Now is the time for all good parties to come to the aid of Man"
all is well if refStringToLocate is "come" but all is NOT well
if refStringToLocate is "time".
refStringToLocate is obviously not in the buffer : otherwise there
would be no need to locate it. But to clarify I am assuming that
refStringToLocate is a seperate buffer.

Doesn't matter; it's the potential overlap in strcpy()
that can cause trouble. Let's consider the "time" case above.
strstr() finds "time" at position 11 in refBuffer, extending
through position 14. strcpy() will then try to copy positions
15-62 (if I've counted correctly, the terminating '\0' is at
position 62) to positions 11-58. Positions 15-58 are therefore
both sources and destinations; that's the overlap. From the
Standard, 7.21.2.3/2: "[...] If copying takes place between
objects that overlap, the behavior is undefined."

The reason for the undefinedness is to allow implementations
maximum freedom to produce a fast and efficient strcpy(); the
Standard does not require that copying take place from left to
right, nor in units of one char at a time. Implementations that
don't (always) go left-to-right or that handle several chars in
each "chunk" can easily get bollixed by overlap, so behavior on
overlap is not specified. There's similar language for memcpy(),
for sprintf(), and for various other things. If you need to
"slide" things within a single array, memmove() is your friend.
 
R

Richard G. Riley

both sources and destinations; that's the overlap. From the
Standard, 7.21.2.3/2: "[...] If copying takes place between
objects that overlap, the behavior is undefined."

Say no more, that's fine. In that case either memmove or the homebrew
code in the previous post. Cheers. For sure that is a rule I've broken
loads of times without knowing any better : and its even in the
manpage! I think, subconsciously, I always thought of "overlap" as only
when the "source" was in front of the dest so that you would
corrupt the "source" as you copied
 
R

Richard G. Riley

Finding a substring is in the library : use strstr(). Also see strcasestr().

The rest is not necessarily clear. You wish to extract the string? By
this maybe "cut" the string out of a null terminated character buffer?

So what you might want to then do is to "strcpy" the rest of the
source string over the start of the string you just located. Below is
untested but you will get the idea:

char * refFound = strstr(refBuffer,refStringToLocate);

if(refFound){
/* assumes null terminated character buffer)
strcpy(refFound, refFound+strlen(refStringToLocate));
}

good luck

See other post : I was in error. And learnt something : strcpy isnt
defined if the strings overlap - there is an outside chance the
platform implementation of strcpy might do the copy backwards or in
another "unknown" manner which would corrupt the source characters. So
the code might look this this instead:

if(refFound){
/* assumes null terminated character buffer)
char * refSource=refFound+strlen(refStringToLocate);
memmove(refFound,refSource,strlen(refSource)+1);
}

or

if(refFound){
/* assumes null terminated character buffer)
char * refSource=refFound+strlen(refStringToLocate;
char * refDest=refFound;
while(*refDest++=*refSource++);
}

I guess the additional strlen() could be potentially cpu expensive on bulk
string manipulations
 
R

Robert Gamble

Polomint said:
If I have a string which holds 'Hello World', how would I extract the
'World' part into another string? Is there a sub string function in C?

Thanks in advance...

Polo

Some languages provide a substring function that returns a new string
provided a source string, offset, and length. The standard C library
does not provide such a function but it is easy enough to create:

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

char * substr(const char *src, size_t start, size_t len);

int main (void) {
char *ptr;
ptr = substr("Hello World", 6, 5);
if (ptr)
printf("%s\n", ptr);
return 0;
}

char * substr(const char *src, size_t start, size_t len) {
char *dest = malloc(len+1);
if (dest) {
memcpy(dest, src+start, len);
dest[len] = 0;
}
return dest;
}

Some caveats:
1. The caller is responsible free()ing the non-null pointer returned
by substr.
2. The function won't operate properly with a len argument of SIZE_MAX
and calling it with such a value may result in undefined behavior.
3. If you specify a start/len combination that is out of bounds of src
you will of course invoke undefined behavior.

It would be trivial to implement error-checking to handle 2 and 3.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top