c++ string + string code example

Example 1: string in cpp

// Include the string library
#include <string>

// Create a string variable
string greeting = "Hello";

Example 2: c++ string

#include <string>

std::string myString = "Hello, World!";

Example 3: c++ strings

#include <iostream>

using namespace std;

void display(char *);
void display(string);

int main()
{
    string str1;
    cout << "Enter a string: ";
    getline(cin, str1);
    display(str1);
    return 0;
}


void display(string s)
{
    cout << "Entered string is: " << s << endl;
}

Example 4: c++ stirng

#include <string>

Tags:

C Example