Clear String C++

Today, we are going to learn one of the important functions of string which clear() function in C++ language.

The principles of object-oriented programming serve as the basis for the C++ programming language (OOP). Since C++ has a simple structure, the user can simply construct and understand the program’s fundamentals. The C++ programming language is exceptionally flexible when it comes to carrying out numerous functions and changing the type of a variable.


A string is a C++ datatype that holds a collection of different characters or objects and is denoted in double quotation marks. The clear() function is one of many functions that the C++ string can carry out and it is used to clear the string in C++. The clear() function is a member of the basic string class in STL (Standard Template Library). In the clear() function, we don’t have to pass any input parameter regardless of the contents of the input string and returns a string with exactly zero characters. The clear() function is the predefined function in C++ which means we don’t have to write dozens of lines of code, we just simply call the function where it is needed. Let’s understand the string clear() function more deeply so that no point of confusion will be left for the reader.


In the prototype of the string clear() function, we simply write the “void” keyword and then we will write the name of the function which we want to implement in this article. We have used “void” as a return type of the clear() function which doesn’t return any value.

In the second prototype of the clear() function, we simply write the declared string variable and concatenate it with the clear() function. We will not pass any parameter in the function brackets because it does not return any value.

Return Value:

In return, this function doesn’t return value.

Example 01:

Let’s start implementing our very first example of the string clear() function in C++ language. For the implementation of the program, we need a C++ compiler so that we can write and execute the program. So, open the C++ compiler and start writing the program.

In C++ language, we always proceed by incorporating the header files required for programs to run properly after opening the compiler. Consequently, we first have included two necessary header files “iostream” and “string” to create the program and use the string clear() method. For handling strings, C++ offers a variety of options.

The “#include <iostream>” is the first library we always use in the C++ program. The “#” sign informs the compiler to load the header file. The term “include” adds the header file into the program, and the “iostream” indicates receiving data from the user and displaying it to the user. We have incorporated the second header file with the prefix “#include <string>” so that strings and string functions can be used across the entire program. We next used the “using namespace std” directive to avoid classes, functions, and variables from using the same namespace throughout the entire program.

#include <iostream>

#include <string>

using namespace std;

int main (){
  string str;
  cout <> str;

  cout << "Input string before clear() function: " << str << endl;
  cout << "The size of the input string is: " << str.size() <<"\n\n";


  cout << "Input string after clear() function: __________" << str << endl;
  cout << "The size of the string is: " << str.size();

  return 0;

After including the header files in the program, we will start the main() function of the program where we write the actual line of code which we want to implement in the program. In line 7, we will declare the string type variable named “str” and then we will get the input from the user by using one of the methods of C++ which is the cin() method. In line 11, we will print the input string “str” which we will recently get from the user by using the cout() method. The cout() method is used to display the output on the console window. Then, we want to print the size of the input string so we have called the size() function which is concatenated with the string variable “str”.

We call the clear() function which is concatenated with the string variable “str” in line 14 so that we can clear the input string that the user has inputted. We again called the cout() method so that we can print the input string after the implementation of the clear() function because we can see whether the input string is empty or not. And again, we will check the size of the string and print it on the output screen. At the end of the main() function, we will return 0 so that the execution of the program will terminate and then we will close the bracket of the main() functions.

Let's see the output of the above-implemented program. As you see there is a “Linux” input string but after the implementation of the clear() function, the blank line is displayed which means there is nothing in the string.

Example 02:

Now, let’s implement another example of the clear() function by using C++ language. Open the C++ IDE and start writing the code. First, we will always include the important header files related to the functions which we are going to use in the program like we want to display the output. For that, we use the will cout() method because it is the predefined function so we include the “iostream” library which is used to input the data and display it to the user. We will use one more library which is the “string” library so that we can use string functions in the whole program. Next, we will write the standard “namespace std” in the program so that we cannot declare the same name for the functions and variables in the program.

We will write the main() function and write the actual code here. In line 8, we declared the string type variable “str”, assigned the string value to it, and printed it. Then, we call the clear() function and then we get the empty string after implementing the clear() function. We have used an if-else statement and we have shown a black string if the if-statement is true and if it is not true, it shows the message that the string is not empty. And then we will return 0 to the main () function.

#include <iostream>

#include <string>

using namespace std;

int main()
    string str = "Welcome To Programming World \n";
    cout<<"The input string before clear() function is: " << str << endl;


    cout<<"The input string after clear() function is: "<< str;

        cout<< "_____________________________";
        cout<< "The string is not empty";

    return 0;

Let's see the output of the above-implemented example and let see what we get:


In this article, we have learned what string clear() functions are in C++ language. We have also learned the writing style and we have implemented some examples of a clear() function with a detailed explanation of every line of code so that no confusion will be left for the user.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.