String Functions

P

Pizzor2000

I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?
 
J

Joona I Palaste

Pizzor2000 said:
I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?

No there isn't. Emulating Right$ is easy: Increment the pointer by the
length of the string minus the number of Right$ characters you want.
Emulating Left$ or Mid$ is more difficult, as you need to modify the
string by putting a '\0' in the middle of it.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
E

Emmanuel Delahaye

Pizzor2000 said:
I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?

Not really, but strncat() can help. Read your C book about it. There is a
little trap...
 
B

Barry Schwarz

I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?
dest = Right$(str, n) can be imitated with something like
strcpy(dest, str+strlen(str)-n);

dest = substr(str, s, e) can be imitated with something like
strncpy(dest, str+s, e-s+1);
dest[e-s+1] = '/0';

Neither of the above deal with "exceptions," such as n exceeding
strlen(str) or n exceeding amount of space at dest or s>e.


<<Remove the del for email>>
 
R

Richard Heathfield

Pizzor2000 said:
I easily forget which string manipulation functions are what in C. Is
there a built-in function to retrieve the substring to the right of a
given character index (like the Right$ function in BASIC),

Yes.

strcpy(target, source + idx);
and/or is there
a function to retrieve the substring from a starting index to an end
index?

Yes.

strncpy(target, source + startidx, endidx + 1 - startidx);
/* strncpy isn't guaranteed to null-terminate
* its output, so let's do that.
*/
target[endidx + 1 - startidx] = '\0';

This is about the only time I'd ever bother using strncpy.

Both calls assume target[] has sufficient storage, so make sure it does.
 
D

Daniel Haude

On Sun, 22 Feb 2004 21:24:49 GMT,
Pizzor2000 said:
I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?

In addition to the answers you already got let me point out that if your
source string is writeable AND you don't need it for anything else, you
can get by just with a bit of pointer artithmetics and marking the end
with a zero. If src is the string from which you want to extract the
substring sub, starting at index start and ending at end, this would look
like:

src[end] = 0;
sub = src+start;

....Plus appropriate boundary checks etc.
 
K

Keith Thompson

Barry Schwarz said:
I easily forget which string manipulation functions are what in C. Is there
a built-in function to retrieve the substring to the right of a given
character index (like the Right$ function in BASIC), and/or is there a
function to retrieve the substring from a starting index to an end index?
dest = Right$(str, n) can be imitated with something like
strcpy(dest, str+strlen(str)-n);

dest = substr(str, s, e) can be imitated with something like
strncpy(dest, str+s, e-s+1);
dest[e-s+1] = '/0';

Neither of the above deal with "exceptions," such as n exceeding
strlen(str) or n exceeding amount of space at dest or s>e.

You meant '\0', not '/0'. (The '/0' form is legal, but it's unlikely
to give you anything useful.)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,310
Messages
2,571,603
Members
48,419
Latest member
EstelaCout

Latest Threads

Top