#include <iostream>
#include <string>//This header of the C++ Standard Library defines the std::string type
/*Type string is a compound type, which is a type that’s a
composite of several data items that are ultimately defined in terms of fundamental types of data. Next to the
characters that make up the string it represents, a string object contains other data as well, such as number
of characters in the string. */
int main()
{
std::string empty; // An empty string that has zero length.
std::string proverb{ "Many a mickle makes a muckle." }; // a string object defined and initialized with a string literal.
/*The character array encapsulated by a string object is always terminated by a null character to assure compatibility
with the numerous existing functions that expect C-style strings.*/
std::cout << proverb.length() <<std::endl; // Outputs 29; This length never includes the string termination character
std::string part_literal{ "Least said soonest mended.", 5 }; // "Least"
std::cout << part_literal << std::endl;
std::string sleeping(6, 'z');//do not use curly braces
std::cout << sleeping << std::endl;//Output zzzzzz
std::string sentence{ proverb };
std::cout << sentence << std::endl;
std::string phrase{ proverb, 0, 13 }; // Initialize with 13 characters starting at index 0
std::cout << phrase << std::endl;
std::string name;
std::cout << "enter your name: ";
std::cin >> name; // Pressing Enter ends input
std::cout << name << std::endl;
/*This reads characters up to the first whitespace character, which ends the input process.*/
std::string adjective{ "adj" }; // Defines adjective
std::string word{ "move" }; // Defines word
word = adjective; // Modifies word
adjective = "beautiful"; // Modifies adjective
std::cout << word << std::endl;
std::cout << adjective << std::endl;
std::string description{ name + " is " + adjective + "." };//Concatenating Strings
/* concatenate string literals with string objects using the + operator. */
/*you can’t concatenate two string literals using the + operator. One of the two operands of the +
operator must always be an object of type string. */
std::cout << description << std::endl;
std::string a_sentence{ "This" " " "is" " " "a" " " "sentence."};
// The compiler will concatenate two or more string literals in sequence into a single literal
std::cout << a_sentence << std::endl;
using namespace std::string_literals;
std::string another_sentence{ "This" + " is a "s + "sentence." };
// appending the letter s to a string literal turns it into a std::string object
std::cout << another_sentence << std::endl;
}
// Concatenating strings with append function
#include <iostream>
#include <string>
int main()
{
std::string sentence{ "The first letter of the English Alphabet is: " }; // Create basic sentence
sentence.append("a");
std::string punctuations("~?!&()");
sentence.append(punctuations, 2, 1); // Appends "!"; index starts from 0
sentence.append(3, '!'); // Appends 3 other "!"
std::cout << sentence << std::endl;
}
#include <iostream>
#include <string>
int main()
{
std::string sentence{ "This sentence is appended with letter:" }; // Create basic sentence
sentence += ',' + ' ';
std::cout << sentence << std::endl; // Output the sentence
/* The ASCII code for ',' is 44, and that of the ' ' character is 32. Their sum, 32 + 44,
therefore equals 76, which happens to be the ASCII code for the capital letter 'L'.*/
}
#include <iostream>
#include <string>
int main()
{
std::string sentence{ "ASCII code 69 is for letter:" }; // E
sentence += 69;
std::cout << sentence << std::endl;
}
#include <iostream>
#include <string>
int main()
{
const std::string result_string{ "Pi is: " };
double result = 3.1415;
std::cout << (result_string + std::to_string(result)) << std::endl;
}
#include <iostream>
#include <string>
#include <cctype> // for std::isalpha() and tolower()
int main()
{
std::string text; // Stores the input
std::cout << "Enter a line of text:\n";
std::getline(std::cin, text); // Read a line including spaces
std::cout << "The line of text you entered is as follows:" << std::endl;
std::cout << text << std::endl;
/*The getline() is declared in the string header. It reads characters from
the stream specified by the first argument, cin in this case, until a newline character is read, and the result is stored in the string object specified by the second argument, which is text in this case.*/
}
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string text; // Stores the input
std::cout << "Enter a line of text:\n";
std::getline(std::cin, tex