copy unknown number of characters from cstring

  • Thread starter Alexandros H. Halatsis
  • Start date
A

Alexandros H. Halatsis

Let's say I have a cstring: char str[] = "Hello world";
and I have a needle: char n[] = "wo";
I find that the needle starts at the 5+1 position of the string.
How am I going to copy the characters up to this point, using the
<cstring> header?
 
V

Victor Bazarov

Alexandros said:
Let's say I have a cstring: char str[] = "Hello world";
and I have a needle: char n[] = "wo";
I find that the needle starts at the 5+1 position of the string.

You do? How?
How am I going to copy the characters up to this point, using the
<cstring> header?

RTFM about 'strncpy' function.

V
 
A

Alexandros H. Halatsis

Alexandros said:
Let's say I have a cstring: char str[] = "Hello world";
and I have a needle: char n[] = "wo";
I find that the needle starts at the 5+1 position of the string.

You do? How?
How am I going to copy the characters up to this point, using the
<cstring> header?

RTFM about 'strncpy' function.

V

Propably it's my fault for not clarifying enough my problem. User
inputs the needle and the string. Let xtetxallnttxxx be the string and
txxx be the needle. Using strstr I get a pointer pointing to the start
of the needle. How am I going to copy the characters up to there?
AFAIK strncpy requires number of characters to copy, but I don't know
the number. Now what?
 
P

Philip Potter

Alexandros said:
Propably it's my fault for not clarifying enough my problem. User
inputs the needle and the string. Let xtetxallnttxxx be the string and
txxx be the needle. Using strstr I get a pointer pointing to the start
of the needle. How am I going to copy the characters up to there?
AFAIK strncpy requires number of characters to copy, but I don't know
the number. Now what?

If 'str' is a char * pointing to the first character of your string, and
'needle' is a char * pointing to the first character of the needle
within that string, then 'needle - str' will give you the number you need.

What are you trying to do? Why are you using C-strings and not
C++-strings? Have you considered what will happen if your needle is not
in the string to be searched?

Phil
 
G

gallows

Alexandros said:
Let's say I have a cstring: char str[] = "Hello world";
and I have a needle: char n[] = "wo";
I find that the needle starts at the 5+1 position of the string.
You do? How?
RTFM about 'strncpy' function.

Propably it's my fault for not clarifying enough my problem. User
inputs the needle and the string. Let xtetxallnttxxx be the string and
txxx be the needle. Using strstr I get a pointer pointing to the start
of the needle. How am I going to copy the characters up to there?
AFAIK strncpy requires number of characters to copy, but I don't know
the number. Now what?

char* str = "hello world";
char* n = "wo";

char* p = std::strstr(str, n);

std::string s;

if (p != NULL)
s = p;
 
A

Alexandros H. Halatsis

If 'str' is a char * pointing to the first character of your string, and
'needle' is a char * pointing to the first character of the needle
within that string, then 'needle - str' will give you the number you need.

What are you trying to do? Why are you using C-strings and not
C++-strings? Have you considered what will happen if your needle is not
in the string to be searched?

Phil

Thanks for the reply. I 'll test it ASAP and report back results.

I 've done it already with C++-strings and I know how to do it.
I 'm doing it now with C-string to get familiar with the c string
functions.

I am sure the needle will be in the string. :)
 
A

Alexandros H. Halatsis

If 'str' is a char * pointing to the first character of your string, and
'needle' is a char * pointing to the first character of the needle
within that string, then 'needle - str' will give you the number you need.

What are you trying to do? Why are you using C-strings and not
C++-strings? Have you considered what will happen if your needle is not
in the string to be searched?

Phil

Thanks for the reply. I 'll test it ASAP and report back results.

I 've done it already with C++-strings and I know how to do it.
I 'm doing it now with C-string to get familiar with the c string
functions.

I am sure the needle will be in the string. :)
 
P

Philip Potter

Alexandros said:
I 've done it already with C++-strings and I know how to do it.
I 'm doing it now with C-string to get familiar with the c string
functions.

A worthy goal. In "real" code (as opposed to "learning" code) I wouldn't
advise using c-strings in C++ at all.
I am sure the needle will be in the string. :)

A wise programmer knows what can't happen, and guards against it anyway.
I have had problems before when something I was sure "can't happen" did
actually happen, and because I hadn't had guard code to protect myself
the errors were bizarre and didn't occur at the point in the code where
the problem was.

Phil
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top