Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Strings
In C++, strings are used to store and manipulate sequences of characters. They provide a convenient way to work with textual data, such as names, sentences, and file contents. C++ offers a powerful string class, std::string
, which provides a wide range of operations and functionalities for string manipulation. In this article, we will explore the usage of strings in C++ with examples.
1. Declaring and Initializing Strings
To use strings in C++, you need to include the <string>
header file. Here's an example that demonstrates declaring and initializing strings:
#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello, World!";
std::string name("John");
std::cout << greeting << std::endl;
std::cout << "My name is " << name << std::endl;
return 0;
}
In the above code, we declare and initialize strings greeting
and name
using different methods. We then print the strings on the console.
2. String Operations
C++ strings provide various operations and functionalities for string manipulation. Here are a few commonly used operations:
- Concatenation: Strings can be concatenated using the
+
operator or theappend()
function. - Length: The
length()
function returns the length of a string. - Accessing Characters: Individual characters in a string can be accessed using indexing.
- Substrings: Substrings can be extracted using the
substr()
function.
Here's an example that demonstrates some of these operations:
#include <iostream>
#include <string>
int main() {
std::string message = "Hello, ";
std::string name = "John";
message += name; // Concatenation
std::cout << message << std::endl;
std::cout << "Length: " << message.length() << std::endl;
std::cout << "First character: " << message[0] << std::endl;
std::string sub = message.substr(7, 4); // Extract substring
std::cout << "Substring: " << sub << std::endl;
return 0;
}
In the above code, we concatenate the string name
to the string message
using the +=
operator. We then demonstrate finding the length of the string, accessing individual characters, and extracting a substring.
3. String Input and Output
C++ allows reading input from the user and displaying output using strings. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::string greeting = "Hello, " + name;
std::cout << greeting << std::endl;
return 0;
}
In the above code, we use the std::cin
object to read input from the user and store it in the string name
. We then concatenate the name with a greeting and display the result using std::cout
.
C++ strings provide a powerful and flexible way to work with textual data. They enable you to manipulate and process strings efficiently. Practice using various string operations, explore additional functionalities provided by the std::string
class, and leverage strings to handle text-based tasks effectively in your C++ programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners