C++ की Strings (C-style)
In this page:
char string_name[size];
char string_name[] = "text"; // ends with a hidden '\0'
C-style String क्या है?
एक C-style string बस एक special convention वाला एक character array है: यह हमेशा एक null-terminator character, '\0', से खत्म होता है, जो mark करता है कि actual text कहाँ रुकता है भले ही array खुद में extra unused space हो।
हर standard C string function यह जानने के लिए उस terminator को ढूंढने पर depend करता है कि कहाँ रुकना है।
उदाहरण: What is a C-style String?
#include <iostream>
int main() {
char greeting[] = "Hi";
std::cout << greeting << std::endl; // stops printing at the null-terminator
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declare और Initialize करना
आप एक C-style string array को explicitly size कर सकते हैं, जैसे char name[20];, बाद में बढ़ने के लिए जगह छोड़ते हुए, या compiler को इसे एक string literal से automatically size करने दें, जैसे char name[] = "Alex";, जो array को exactly text plus terminator में fit करने के लिए size करता है।
उदाहरण: Declaring and Initializing
#include <iostream>
#include <cstring>
int main() {
char name[20];
strcpy(name, "Alex");
char city[] = "Delhi"; // compiler sizes this automatically
std::cout << name << " " << city << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Null-Terminator (\0)
Null-terminator ही वह है जो एक C-style string को characters के एक ordinary array से अलग करता है — इसके बिना, strlen() या cout << जैसे functions को यह जानने का कोई तरीका नहीं होगा कि meaningful text कहाँ खत्म होता है और बचा हुआ, uninitialized memory कहाँ शुरू होती है, और intended end से आगे पढ़ते (और शायद crash या garbage print करते) रहेंगे।
उदाहरण: The Null-Terminator (\0)
// Include std::cout and std::cin
#include <iostream>
#include <cstring>
// Program execution starts in main()
int main() {
char word[] = {'H', 'i', '\0'};
// Print to the console with cout
std::cout << word << " length=" << strlen(word) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Characters Access और Modify करना
एक C-style string के अंदर individual characters को ordinary array indexing इस्तेमाल करके पढ़ा या overwrite किया जा सकता है, जैसे name[0] = J;, exactly जैसे आप किसी दूसरे char array के साथ करते — बस terminating '\0' को कभी overwrite या accidentally remove न करने के लिए careful रहें।
उदाहरण: Accessing and Modifying Characters
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
char name[] = "jack";
name[0] = 'J';
// Print to the console with cout
std::cout << name << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C-style Strings को Safely पढ़ना
cin >> से सीधे एक C-style string में input पढ़ना पहले whitespace character पर रुकता है, जो spaces रखने वाली किसी भी चीज़ के लिए fail होता है — cin.getline(buffer, size) इसकी बजाय एक specified maximum length तक एक पूरी line पढ़ता है, अगर input उम्मीद से लंबा हो तो एक buffer overflow safely avoid करते हुए।
उदाहरण: Reading C-style Strings safely
#include <iostream>
int main() {
char fullName[50] = "Alex Johnson"; // cin >> would stop at the space
std::cout << fullName << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- null terminator के लिए जगह भूल जाना, जैसे
char s[5] = "Hello";, जो fit नहीं होता। - C-strings को
==से compare करना, जो contents नहीं addresses compare करता है;strcmpइस्तेमाल करें। - एक array को
s = "Hi"से assign करना, जो allowed नहीं है;strcpyइस्तेमाल करें।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings