String sample code snippet

Summary

Source code of C++ program, demonstrating very basic code working with C++ strings.

Source code

C++: string_sample.cpp

#include <iostream>

#include <string>

 

using namespace std;

 

int main() {

      // string is a class allowing

      // to work with text data in C++

 

      // string variable name

      string name;

     

      cout << "Enter your name: ";

      // read to string

      cin >> name;

 

      // write string to the console

      cout << "Hello, " << name << "!" << endl;

     

      return 0;

}

 

Sample run

C/C++

Enter your name: John
Hello, John!
 

Partners Ads

electric fence