Solved

Strings HELP (NOT MFC)

Posted on 2003-11-26
5
230 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
Comment Utility
sorry

String2 =10.5.99.99
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now