Define a node listener that subscribes to the topic, publish to the topic chatter, and the message type is a string. The publisher code link of the topic : ROS release topic C++ code detailed explanation
#include "ros/ros.h"
#include "std_msgs/String.h"
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 10, chatterCallback);
ros::spin();
}