Extracting a substring

V

Vicent Giner-Bosch

Hello. This is a basic question, but I haven't been able to find out
the answer by my own.

I have a variable which is a char array, I mean:


char line_p[80] ;


How can I get the substring from the 1st character to the 10th (for
example) and store it in another char array ??

I've found the method "substring" or something similar, but that's for
"string" objects, which is not my case.

Thank you in advance!
 
F

Fred

Hello. This is a basic question, but I haven't been able to find out
the answer by my own.

I have a variable which is a char array, I mean:

    char line_p[80] ;

How can I get the substring from the 1st character to the 10th (for
example) and store it in another char array ??

I've found the method "substring" or something similar, but that's for
"string" objects, which is not my case.
If you really must use a char array (C-style string) instead of a
string, look at these:
strncpy
memcpy
memmove
 
V

Vicent Giner-Bosch

If you really must use a char array (C-style string) instead of a
string, look at these:
strncpy
memcpy
memmove

Well, I prefer using C-style strings.

I've understood the use of strncpy, thanks.

Two more questions:

(1) What if I want to get the substring from the 3rd to the 7th
position (for example)?

(2) Is there any "wrap" function for C-style strings, I mean, any
function for taking away the white spaces at the beginning at the end
of a string?

Thank you in advance.
 
A

Andrew Poelstra

Well, I prefer using C-style strings.

Then use C. C++ has much stricter typing than C, which
forces you to cast the return value of malloc (a risky
practice), and in general use more casts than is safe.

Also, you have to track string length yourself, which
breaks encapsulation and is also often slower.
 

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
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top