🟡 Control Flow  ·  Lesson 22

Strings in C++

Strings in C++

A string is text — a sequence of characters. Modern C++ gives you the std::string type (from <string>), which manages the text and its memory for you. It is far easier than the old C-style character arrays.

Creating strings

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Amit";
    string empty;              // starts as ""
    cout << name << "\n";
    return 0;
}
Output:
Amit

Reading string input

Use getline to read a full line with spaces; cin >> reads only one word:

C++
string full;
cout << "Enter full name: ";
getline(cin, full);
cout << "Hello, " << full << "\n";
Run: Enter full name: Amit Kumar
Output:
Hello, Amit Kumar

Joining strings

Use + or += to join strings — no special functions needed:

C++
string first = "Amit", last = "Kumar";
string full = first + " " + last;
full += "!";
cout << full;
Output:
Amit Kumar!

Length and characters

C++
string s = "Hello";
cout << s.length() << "\n";   // 5
cout << s[0] << "\n";         // H
s[0] = 'J';                   // change a character
cout << s;                     // Jello
Output:
5
H
Jello

Useful methods

MethodWhat it does
s.length() / s.size()Number of characters
s.substr(pos, len)Extract part of the string
s.find("x")Position of a substring (or npos)
s.empty()True if the string has no characters
s.append("x")Add to the end

A note on C-style strings

C++ also supports old C-style strings — arrays of char ending in a '\0' null character, used with functions from <cstring>. You may see them in older code, but std::string is safer and preferred for new programs.

Common mistakes

  • Forgetting #include <string> when using std::string.
  • Using cin >> s for a full name and capturing only one word.
  • Mixing std::string and C-style functions incorrectly.
  • Going out of range with s[i]; use .at(i) for a checked access.
🏋️ Practice

Read a full name with getline, then print its length, its first character, and the name in the form "Last, First" using substr and find.

Summary

  • Use std::string from <string> for text.
  • getline(cin, s) reads a whole line; cin >> s reads one word.
  • Join strings with + and +=.
  • .length() gives the size; s[i] accesses characters.
  • Prefer std::string over C-style char arrays in new code.

Frequently Asked Questions

What is a string in C++?
In modern C++ a string is an object of the std::string type from the <string> header, which stores and manages text for you. It can grow and shrink automatically, unlike the older C-style character arrays, making text handling much easier and safer.
How do you read a string with spaces in C++?
Use getline(cin, str), which reads a whole line including spaces until you press Enter. The extraction operator cin >> str stops at the first space, so it only captures one word.
How do you concatenate strings in C++?
With std::string you simply use the + operator or +=, for example string full = first + " " + last;. This joins the pieces into one new string, which is far simpler than the functions needed for C-style strings.
How do you find the length of a string in C++?
Call .length() or the equivalent .size() on a std::string, such as name.length(). Both return the number of characters, and you can access individual characters with name[i] or name.at(i).
What is the difference between std::string and a C-style string in C++?
A std::string is a managed object that handles its own memory and size, while a C-style string is a plain array of char ending in a null character. std::string is safer and easier, so it is preferred unless you are interfacing with older C code.

C++ में strings

String text है — characters का एक क्रम। आधुनिक C++ आपको std::string type देता है (<string> से), जो आपके लिए text और उसकी memory प्रबंधित करता है। यह पुरानी C-शैली के character arrays से कहीं आसान है।

Strings बनाना

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Amit";
    string empty;              // "" se shuru hota hai
    cout << name << "\n";
    return 0;
}
Output:
Amit

String input पढ़ना

Spaces वाली पूरी line पढ़ने को getline इस्तेमाल करें; cin >> केवल एक शब्द पढ़ता है:

C++
string full;
cout << "Enter full name: ";
getline(cin, full);
cout << "Hello, " << full << "\n";
Run: Enter full name: Amit Kumar
Output:
Hello, Amit Kumar

Strings जोड़ना

Strings जोड़ने को + या += इस्तेमाल करें — कोई विशेष functions नहीं चाहिए:

C++
string first = "Amit", last = "Kumar";
string full = first + " " + last;
full += "!";
cout << full;
Output:
Amit Kumar!

Length और characters

C++
string s = "Hello";
cout << s.length() << "\n";   // 5
cout << s[0] << "\n";         // H
s[0] = 'J';                   // ek character badlein
cout << s;                     // Jello
Output:
5
H
Jello

उपयोगी methods

Methodयह क्या करता है
s.length() / s.size()Characters की संख्या
s.substr(pos, len)String का हिस्सा निकालें
s.find("x")Substring की स्थिति (या npos)
s.empty()True अगर string में कोई character न हो
s.append("x")अंत में जोड़ें

C-style strings पर एक नोट

C++ पुरानी C-style strings भी समर्थन करता है — char के arrays जो '\0' null character पर खत्म होते हैं, <cstring> के functions के साथ इस्तेमाल। आप इन्हें पुराने code में देख सकते हैं, पर नए programs के लिए std::string सुरक्षित और पसंदीदा है।

आम गलतियाँ

  • std::string इस्तेमाल करते समय #include <string> भूलना।
  • पूरे नाम के लिए cin >> s इस्तेमाल करना और केवल एक शब्द पकड़ना।
  • std::string और C-style functions को गलत तरीके से मिलाना।
  • s[i] से range के बाहर जाना; जाँची गई access के लिए .at(i) इस्तेमाल करें।
🏋️ अभ्यास

एक पूरा नाम getline से पढ़ें, फिर उसकी length, उसका पहला character, और नाम "Last, First" रूप में substr तथा find इस्तेमाल करके print करें।

सारांश

  • Text के लिए <string> से std::string इस्तेमाल करें।
  • getline(cin, s) पूरी line पढ़ता है; cin >> s एक शब्द।
  • Strings को + और += से जोड़ें।
  • .length() size देता है; s[i] characters access करता है।
  • नए code में C-style char arrays के बजाय std::string पसंद करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.