String Input and Output in Cpp
String Input and Output
A. Reading Strings from User Input
In C++, you can read strings from user input using the std::cin object along with the >> extraction operator or the std::getline() function. Examples:
Reading a single word as a string:
std::string word;
std::cout << "Enter a word:";
std::cin >> word;
Reading a line of text as a string:
std::string line;
std::cout << "Enter a sentence: ";
std::getline(std::cin, line);
Note that when using std::getline(), it is important to clear the input buffer before calling it, especially if you have previously used std::cin to read other data types.
B. Displaying Strings as Output
To display strings as output in C++, you can use the std::cout object and the << insertion operator. Examples:
Displaying a string:
std::string message = "Hello, world!";
std::cout << message;
Concatenating strings and other data types in the output:
std::string name = "John";
int age = 25;
std::cout << "My name is " << name << " and I am " << age << " years old.";
The output will be:
The output will be: My name is John and I am 25 years old.
You can also use the std::endl manipulator to insert a newline after displaying the string, or use the escape sequence to insert a newline character manually.
Note that C++ provides various formatting options and manipulators, such as std::setw(), std::setprecision(), etc., which allow you to control the formatting and alignment of the output when working with strings and other data types.
String Operations and Functions
A. Comparing Strings
In C++, you can compare strings using various methods, such as the relational operators (==, !=, <, >, <=, >=), the compare() member function, or the == operator of the std::string class. Examples:
Using relational operators:
std::string str1 = "Hello";
std::string str2 = "World";
if (str1 == str2) {
// Strings are equal
} else if (str1 < str2) {
// str1 is less than str2
} else {
// str1 is greater than str2
}
Using compare() member function:
std::string str1 = "Hello";
std::string str2 = "World";
int comparison = str1.compare(str2);
if (comparison == 0) {
// Strings are equal
} else if (comparison < 0) {
// strl is less than str2
} else {
// strl is greater than str2
}
B. Checking for Empty Strings
To check if a string is empty (contains no characters), you can use the empty() member function or compare the length of the string to zero. Examples:
std::string str = "Hello";
if (str.empty()) {
// String is empty
} else {
// String is not empty
}
if (str.length() == 0) {
// String is empty
} else {
// String is not empty
}
C. Converting Strings to Numbers
In C++, you can convert strings to numeric types using functions like std::stoi(), std::stof(), std::stod(), etc. Examples:
std::string str = "123";
int number = std::stoi(str); // Convert string to integer
std::string str2 = "3.14";
float floating Number = std::stof(str2); // Convert string to float
D. Converting Numbers to Strings
To convert numeric values to strings in C++, you can use the std::to_string() function.
E. Formatting Strings
To format strings in C++, you can use various techniques, including string concatenation, string interpolation with the + operator, or utilizing formatting libraries like printf-style formatting or the std::stringstream class.
Note : The C++ Standard Library also provides additional formatting capabilities through the