use string in c++ code example

Example 1: string in cpp

// Include the string library
#include <string>

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

Example 2: declaring strings c++

std::string str = "hello world"; 
char *str = "hello world";
char str[] = "hello world"; 
char str[11] = "hello world";

Example 3: c++ string

#include <string>

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

Example 4: string in cpp

// you want to include <string>
#include <string>
#include <iostream>

int main() 
{
  string helloWorld = "Hello World!"; // creating string and assigning
  std::cout << helloWorld;            // will output what you assigned it!
                                      /* you can also use strings with user 
                                      input (cin/getline)*/
  string namePerson{};
  getline(cin, namePerson); // getline allows for multi word input
  std::cout << namePerson;  // outputs name which person inputted
}

Example 5: c++ write string

#include <iostream>

int main() {
	std::cout << "Hello" << std::endl; //endl = end line/new line
    // or
    printf("hello");
    
}

Tags:

C Example