string function c++ code example

Example 1: string in cpp

// Include the string library
#include <string>

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

Example 2: string in c++

#include <iostream>
#include <string>//for printing std::string
int main()

{
	//A string is a group of characters and an array of const chars
	const char* name = "Caleb";//C style string
	//how string actually works below:
	//String without null terminating character below:

	char name2[5] = { 'C','a','l','e','b' };// string is just an array of characters
	//The above doesn't have an null termination character at the end cout will not now where the string ends and will acess memory that is not a part of your string
	std::cout << name2 << std::endl;//output => Caleb + somejunk //this is because null terminating char is not present at the end of array
	//String with null terminating character below:

	char name3[6] = { 'C','a','l','e','b','\0' };//null terminating char '\0' or '0' can be used 
	std::cout << name3 << std::endl;//output => Caleb // because null terminating char is present cout knows where array ends 

	//std::string class in c++ is takes an array of const chars and a bunch of functions to manuplate it:
	//std::string has a constructor  that takes const char array
	std::string name4 = "Caleb";
	name4.size();//gives size of string and there are many more methods in std::string class
	
  //appending to std::string
  
  	//"Ever thing inside these double quotes becomes const char array"
   //std::string namee = "Caleb" +"Hello";//This will give error because adding const char array to const char array 
	std::string namee = "Caleb";
	namee += " Hello";//This will work because adding a ptr to a actual string
	std::cout << namee << std::endl;
//You can also use the below
	std::string namee2 = std::string("Caleb")+" Hello";// This will work because constructor will convert const char array  to string, adding a ptr to string
	std::cout << namee2 << std::endl;
    std::cin.get();

}

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++ string manipulation

/*
OUTPUT
String value: Programming    {where applicable: -31.05}
-----------------------------------------------------------------------------
|Programming|            << [No manipulation]
|         Programming|   << [Width:20][align:string_default=right]
|         Programming|   << [Width:20][align:right]
|Programming         |   << [Width:20][align:left]
|-31.05              |   << [Width:20][align:int_default=left]
|-              31.05|   << [Width:20][align:internal]
|.........Programming|   << [Width:20][align:default][fill:.]
|+++++++++Programming|   << [Width:20][align:default][fill:+]
|=========Programming|   << [Width:20][align:default][fill:=]
-----------------------------------------------------------------------------
*/

string sString = "Programming"; // Length = 11 
//NOTE: always place the settings before the actual string in cout

// width
cout << "|" << sString << "|" << "\t\t << [No manipulation]" << endl;
cout << "|" << setw(20) << sString << "|" << "\t << [Width:20][align:string_default=right]\n";

// alignment
cout << "|" << setw(20) << right << sString << "|" << "\t << [Width:20][align:right]\n";
cout << "|" << setw(20) << left << sString << "|" << "\t << [Width:20][align:left]\n";
cout << "|" << setw(20) << -31.05 << "|" << "\t << [Width:20][align:int_default=left]\n";
cout << "|" << setw(20) << internal << -31.05 << "|" << "\t << [Width:20][align:internal]\n";

// fill (HAVE to use single quotes in the setfill argument definition)
cout << "|" << setw(20) << setfill('.') << sString << "|" << "\t << [Width:20][align:default][fill:.]\n";
cout << "|" << setw(20) << setfill('+') << sString << "|" << "\t << [Width:20][align:default][fill:+]\n";
cout << "|" << setw(20) << setfill('=') << sString << "|" << "\t << [Width:20][align:default][fill:=]\n";

Example 6: 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;
}

Tags:

C Example