473,805 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy unknown number of characters from cstring

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
<cstringheade r?

--
Using <string(c++ string) it is possible in two ways:
1) for (int counter = 0; i < str.find(n); i++)
tmp += str[i];
2) str.substr();

Oct 29 '07 #1
8 2590
Alexandros H. Halatsis wrote:
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
<cstringheade r?
RTFM about 'strncpy' function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '07 #2
On Oct 29, 5:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Alexandros H. Halatsis wrote:
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
<cstringheade r?

RTFM about 'strncpy' function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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?

Oct 29 '07 #3
Alexandros H. Halatsis wrote:
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
Oct 29 '07 #4
On 29 Ott, 17:37, "Alexandros H. Halatsis" <stream...@gmai l.com>
wrote:
On Oct 29, 5:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Alexandros H. Halatsis wrote:
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
<cstringheade r?
RTFM about 'strncpy' function.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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;

Oct 29 '07 #5
On Oct 29, 7:01 pm, Philip Potter <p...@see.sig.i nvalidwrote:
Alexandros H. Halatsis wrote:
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
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. :-)

Oct 29 '07 #6
On Oct 29, 7:01 pm, Philip Potter <p...@see.sig.i nvalidwrote:
Alexandros H. Halatsis wrote:
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
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. :-)

Oct 29 '07 #7
Alexandros H. Halatsis wrote:
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
Oct 30 '07 #8
On 29 Oct, 17:25, "Alexandros H. Halatsis" <stream...@gmai l.com>
wrote:
On Oct 29, 7:01 pm, Philip Potter <p...@see.sig.i nvalidwrote:
Alexandros H. Halatsis wrote:
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?
<snip>
I am sure the needle will be in the string. :-)
put an assert() in then
--
Nick Keighley
Oct 30 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3504
by: Dominique | last post by:
Hi Can anyone help here. I have defined a copy constructor: CString::CString (const CString &string) // copy constructor { Int16 size = string.GetLength() + 1; // this line generates error .... }
42
5816
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
79
14128
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current operating system would be appropriate. However, I do not know how to retrieve the information. Could you help? Klaus
27
2302
by: Shagy | last post by:
Greetings, I've been trying to find an equivant c funtion to the c++ copy function. Description: copy(char *cstring, size_t count, size_t offset); Copies "count" characters from a C-style string starting at offset.
10
9162
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
4
6902
by: K | last post by:
I've an XML file in UTF-8. It contains some chinese characters ( both simplified chinese and traditional chinese). In loading the XML file with MSXML parser, I used the below code to retrieve the data in a node. The CString was then display in CListCtrl. For the traditional chinese characters, they were shown correctly, but for simplified characters, I encounted many "?", but some characters were correct.
9
2739
by: muzmail | last post by:
I have declared a copy constructor for a template class in a Visual C++ project but for some reason the compiler ignores it. I can put syntax errors in the copy constructor and the compiler ignores them. So what's the problem with my code? #ifndef threedeematrix_h #define threedeematrix_h
111
20084
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- http://www.riscossione.info/
4
4039
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find and open an unknown workbook that contains the data that I need to imput into my current workbook, once the unknown workbook is opened it should automatically select my range (number of columns known, but number of rows unknown- the number of rows...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.