Add Bold Tag in String in C++



 A string is a sequence of characters, like words, numbers, or sentences, enclosed in double quotes ("). The given task is to add a closed pair of HTML bold tags around a string using C++.

Let us understand this with an example:

Input: abc
Output: <b>abc</b>

Add Bold Tag in String in C++

To add bold tags to a string in C++, we use string concatenation. This is the process of joining two or more strings together to form a new string. In C++, this can be done using the '+' operator.

To wrap a string with bold tags, we concatenate the <b> tag before the string and the </b> tag after it.

Example

The following is a C++ program where we take a string, concatenate it with bold tags, and display the result:

#include <iostream>
#include <string>

int main() {
   // Step 1: Define the original string
   std::string text = "Welcome to TutorialsPoint";

   // Step 2: Concatenate <b> tag before and </b> tag after the string
   std::string boldText = "<b>" + text + "</b>";

   // Step 3: Output the result
   std::cout << boldText << std::endl;
   return 0;
}

Once you run the above program, it will show the input string with <b> tags added:

<b>Welcome to TutorialsPoint</b>

Time Complexity: O(n) because we loop through the string to add the bold tags.

Space Complexity: O(n) because a new string is created to store the result.

Conclusion

In this article, we learned how to add HTML bold tags to a string in C++ using string concatenation with the + operator. This method is simple and efficient, with both time and space complexities being O(n), where n is the length of the string.

Updated on: 2025-07-25T13:00:47+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements