Solved

Strings HELP (NOT MFC)

Posted on 2003-11-26
5
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 135
sorting efficency of sorting algorithm 30 151
C++ mouse_event mouse look 7 134
Create a worker thread ( servicing thread) using the Message queue 3 49
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 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