Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strings HELP (NOT MFC)

Posted on 2003-11-26
5
Medium Priority
?
239 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
4 Comments
 

Author Comment

by:LPlate
ID: 9826370
sorry

String2 =10.5.99.99
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

876 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