Zoe Leullier
Zoe Leullier

Reputation: 46

How to add a char* inside of a char*?

How do I add, for example, message, inside of this char*?

add_message(char* message) {
   char* json = "{\"message\": \"%%insert message here%%\"}"
}

I'm really new to C++ and don't know any terms, so forgive me if this is too simple.

Upvotes: 0

Views: 114

Answers (1)

Hilton Fernandes
Hilton Fernandes

Reputation: 660

Just to concatenate the wisdom presented in the comments, and to ease the transition of the new C++ programmer, here is a tiny (and silly) sample code,

#include <iostream> 
#include <string>

std::string add_message(const std::string& body) {
    std::string json = "{\"message\": \"%%" + body + "%%\"}";

    return json;
}

int main()
{
    std::string body = "This is a message body"; 

    std::cout << "For the body '" << body 
              << "' the JSON msg is '" << add_message(body) << "'" 
              << std::endl; 

    return 0;
}

Upvotes: 1

Related Questions