How to Print a String in C++

  1. Use std::cout and << Operator to Print a String
  2. Use std::copy Algorithm to Print a String
  3. Use printf() Function to Print a String

This article will demonstrate multiple methods about how to print a string in C++.

Use std::cout and << Operator to Print a String

std::cout is the global object for controlling the output to a stream buffer. To output the s1 string variable to the buffer, we need to use the operator - << called the stream insertion operator. The following example demonstrates a single string output operation.

#include <iostream>
#include <string>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string s1 = "This string will be printed";
    cout << s1;
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be printed

Use std::copy Algorithm to Print a String

copy method is from the <algorithm> STL library, and it can manipulate the range elements in multiple ways. Since we can access the string container itself as a range, we can output each element by adding the std::ostream_iterator<char> argument to the copy algorithm.

Notice that this method can also pass a specific delimiter between each character of the string. In the following code, no delimiter ("") is specified to print string in original form.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::copy;

int main(){
    string s1 = "This string will be printed";

    copy(s1.begin(), s1.end(),
         std::ostream_iterator<char>(cout, ""));
    cout << endl;

    return EXIT_SUCCESS;
}

Use printf() Function to Print a String

printf is a powerful tool used for formatted output. It is part of C standard input-output library. It can be called from C++ code directly. printf has the variable number of parameters, and it takes string variable as char * type, which means we have to call the c_str method from s1 variable to pass it as an argument. Note that each type has its own format specifier (e.g. string - %s), which are listed in the following table.

Specifier Description
% Prints a literal % character (this type doesn’t accept any flags, width, precision, length fields).
d, i int as a signed integer. %d and %i are synonymous for output, but are different when used with scanf for input (where using %i will interpret a number as hexadecimal if it’s preceded by 0x, and octal if it’s preceded by 0.)
u Print decimal unsigned int.
f, F double in normal (fixed-point) notation. f and F only differ in how the strings for an infinite number or NaN are printed (inf, infinity and nan for f; INF, INFINITY and NAN for F).
e, E double value in standard form (d.ddde±dd). An E conversion uses the letter E (rather than e) to introduce the exponent.
g, G double in either normal or exponential notation, whichever is more appropriate for its magnitude. g uses lower-case letters, G uses upper-case letters.
x, X unsigned int as a hexadecimal number. x uses lower-case letters and X uses upper-case.
o unsigned int in octal.
s null-terminated string.
c char (character).
p void* (pointer to void) in an implementation-defined format.
a, A double in hexadecimal notation, starting with 0x or 0X. a uses lower-case letters, A uses upper-case letters.
n Print nothing, but writes the number of characters successfully written so far into an integer pointer parameter.
#include <iostream>
#include <string>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string s1 = "This string will be printed";
    cout << s1;
    cout << endl;

    printf("%s", s1.c_str());
    cout << endl;

    return EXIT_SUCCESS;
}

Related Article - C++ String

  • How to Convert String to Lower Case in C++
  • How to Convert String to Char Array in C++