Solved

Strings HELP (NOT MFC)

Posted on 2003-11-26
5
235 Views
Last Modified: 2010-04-02
Hi People,

I have two strings and need to create one string with the data from string1 and string2 separated by a null terminator. I'm developing eVC++ 3.0

I have listed what I’m trying to achieve below...... :->

String1 = const CHAR newipStr [] = "Kx001";
String2 = char IpNew[34];

String2 = “10.5.99.99”

String3 = String1 + NULL TERMINATOR + String2

String3 = Kx001(NT)10.5.99.99

(NT) = NULL TERMINATOR

Thanks
0
Comment
Question by:LPlate
5 Comments
 

Author Comment

by:LPlate
ID: 9826370
sorry

String2 =10.5.99.99
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 9826412
const char* string1 = "Kx001";
const char* string2 = "10.5.99.99";
char IpNew[34];

int string1size = strlen ( string1) + 1; //including it's NT
int string2size = strlen ( string2) + 1; //including it's NT

memset ( IpNew, 0, sizeof ( IpNew));

memcpy ( IpNew, string1, string1size);

memcpy ( IpNew + string1size, string2, string2size);


0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9831008
If you need to embed null terminators into strings, you may find it easier to use std::string instead of char[]. std::string doesn't treat the '\0' character as anything special (except where the constructor taking a const char* with no length is used).

--------8<--------
#include <iostream>
#include <string>

int main()
{
        const char c_string1[] = "Kx001";
        const char c_string2[] = "10.5.99.99";
        const std::string string1(c_string1,sizeof(c_string1)); // Include the '\0'
        const std::string string2(c_string2); // Don't include the '\0'

        std::string string3 = string1+string2;

        std::cout << "Here it is [" << string3 << "]\nRedirect output to a file to see the '\\0' character!\n";

        const char *data = string3.data();
        std::string::size_type size = string3.size();
        std::cout << std::hex;
        while (size--)
                std::cout << "Character is 0x" << static_cast<int>(*data++) << '\n';
}
--------8<--------
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286134
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9826412}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question