String and Character Functions
Built-in functions that make working with text easier.
String and Character Functions
C++ में strings और characters पर विभिन्न operations (क्रियाएँ) करने के लिए कई built-in functions (पूर्वनिर्मित functions) उपलब्ध हैं। इन functions की सहायता से हम string की length पता कर सकते हैं, characters को search कर सकते हैं, string को modify कर सकते हैं और characters का प्रकार भी check कर सकते हैं।
string class के functions तथा characters पर operations के लिए <cctype> functions का उपयोग किया जाता है।
String Functions
C++ की string class में कई useful member functions होते हैं। इनका उपयोग strings को manipulate (परिवर्तित) और process करने के लिए किया जाता है।
| Function | Use |
|---|---|
length() |
String की length पता करना |
size() |
String में characters की संख्या पता करना |
append() |
String के अंत में दूसरी string जोड़ना |
find() |
Character या substring को search करना |
substr() |
String का एक हिस्सा प्राप्त करना |
insert() |
String में characters insert करना |
erase() |
String से characters हटाना |
replace() |
String के किसी हिस्से को बदलना |
1. length() Function
length() function का उपयोग string में मौजूद characters की संख्या ज्ञात करने के लिए किया जाता है।
Syntax
string_name.length();
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Computer";
cout << word.length();
return 0;
}
8
यहाँ Computer में कुल 8 characters हैं।
2. size() Function
size() function भी string में मौजूद characters की संख्या प्राप्त करने के लिए उपयोग किया जाता है।
Syntax
string_name.size();
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Hello";
cout << word.size();
return 0;
}
5
length() और size() दोनों string में मौजूद characters की संख्या return करते हैं।
3. append() Function
append() function का उपयोग किसी existing string के अंत में दूसरी string जोड़ने के लिए किया जाता है।
Syntax
string1.append(string2);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string first = "Hello";
first.append(" World");
cout << first;
return 0;
}
Hello World
4. find() Function
find() function का उपयोग string में किसी character या substring की position खोजने के लिए किया जाता है।
Syntax
string_name.find(value);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Computer";
cout << word.find("put");
return 0;
}
3
यहाँ substring "put" index 3 से शुरू होती है। यदि searched value नहीं मिलती है, तो find() सामान्यतः string::npos return करता है।
5. substr() Function
substr() function का उपयोग किसी string के एक particular part को प्राप्त करने के लिए किया जाता है।
Syntax
string_name.substr(start_position, length);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Computer";
cout << word.substr(0, 4);
return 0;
}
Comp
यहाँ substr(0, 4) index 0 से शुरू करके 4 characters return करता है।
6. insert() Function
insert() function का उपयोग string की किसी particular position पर characters या दूसरी string insert करने के लिए किया जाता है।
Syntax
string_name.insert(position, value);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Hello World";
word.insert(6, "C++ ");
cout << word;
return 0;
}
Hello C++ World
7. erase() Function
erase() function का उपयोग string से characters के किसी भाग को remove (हटाने) के लिए किया जाता है।
Syntax
string_name.erase(position, length);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Hello World";
word.erase(5, 6);
cout << word;
return 0;
}
Hello
8. replace() Function
replace() function का उपयोग string के किसी part को दूसरी string से replace करने के लिए किया जाता है।
Syntax
string_name.replace(position, length, new_string);
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "I like Java";
word.replace(7, 4, "C++");
cout << word;
return 0;
}
I like C++
Character Functions
Characters पर विभिन्न operations करने के लिए C++ में <cctype> header file के कई functions उपलब्ध हैं। ये functions यह पता लगाने में मदद करते हैं कि कोई character alphabet, digit, uppercase, lowercase आदि है या नहीं।
#include <cctype> header file include की जाती है।
Common Character Functions
| Function | Purpose |
|---|---|
isalpha() |
Character alphabet है या नहीं check करना |
isdigit() |
Character digit है या नहीं check करना |
isalnum() |
Character alphabet या digit है या नहीं check करना |
islower() |
Character lowercase है या नहीं check करना |
isupper() |
Character uppercase है या नहीं check करना |
isspace() |
Character whitespace है या नहीं check करना |
tolower() |
Uppercase character को lowercase में बदलना |
toupper() |
Lowercase character को uppercase में बदलना |
1. isalpha() Function
isalpha() function check करता है कि दिया गया character alphabetic character है या नहीं। यदि character alphabet है तो यह non-zero value return करता है, अन्यथा zero return करता है।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'A';
if (isalpha(ch))
{
cout << "Alphabet";
}
else
{
cout << "Not an alphabet";
}
return 0;
}
Alphabet
2. isdigit() Function
isdigit() function check करता है कि character digit है या नहीं। Digits 0 से 9 तक होते हैं।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = '7';
if (isdigit(ch))
{
cout << "Digit";
}
else
{
cout << "Not a digit";
}
return 0;
}
Digit
3. isalnum() Function
isalnum() function check करता है कि character alphabet या digit है या नहीं। यदि character letter या digit है, तो यह non-zero value return करता है।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = '9';
if (isalnum(ch))
{
cout << "Alphanumeric";
}
else
{
cout << "Not alphanumeric";
}
return 0;
}
Alphanumeric
4. islower() Function
islower() function check करता है कि character lowercase alphabet है या नहीं।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'm';
if (islower(ch))
{
cout << "Lowercase";
}
else
{
cout << "Not lowercase";
}
return 0;
}
Lowercase
5. isupper() Function
isupper() function check करता है कि character uppercase alphabet है या नहीं।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'M';
if (isupper(ch))
{
cout << "Uppercase";
}
else
{
cout << "Not uppercase";
}
return 0;
}
Uppercase
6. isspace() Function
isspace() function check करता है कि character whitespace character है या नहीं। Space, tab और newline जैसे characters whitespace के उदाहरण हैं।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = ' ';
if (isspace(ch))
{
cout << "Whitespace";
}
else
{
cout << "Not whitespace";
}
return 0;
}
Whitespace
7. tolower() Function
tolower() function uppercase alphabetic character को lowercase में convert करने के लिए उपयोग किया जाता है।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'A';
cout << (char)tolower(ch);
return 0;
}
a
8. toupper() Function
toupper() function lowercase alphabetic character को uppercase में convert करने के लिए उपयोग किया जाता है।
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'b';
cout << (char)toupper(ch);
return 0;
}
B
Converting a String to Uppercase
Loop और toupper() function का उपयोग करके string के सभी characters को uppercase में convert किया जा सकता है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string word = "computer";
for (int i = 0; i < word.length(); i++)
{
word[i] = toupper(word[i]);
}
cout << word;
return 0;
}
COMPUTER
Converting a String to Lowercase
इसी प्रकार tolower() का उपयोग करके string के characters को lowercase में convert किया जा सकता है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string word = "PROGRAMMING";
for (int i = 0; i < word.length(); i++)
{
word[i] = tolower(word[i]);
}
cout << word;
return 0;
}
programming
Counting Digits in a String
isdigit() function का उपयोग करके string में मौजूद digits की संख्या count की जा सकती है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text = "Class11Computer2026";
int count = 0;
for (int i = 0; i < text.length(); i++)
{
if (isdigit(text[i]))
{
count++;
}
}
cout << "Digits = " << count;
return 0;
}
Digits = 4
Counting Alphabets in a String
isalpha() function की सहायता से string में alphabetic characters की संख्या count की जा सकती है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text = "Class11";
int count = 0;
for (int i = 0; i < text.length(); i++)
{
if (isalpha(text[i]))
{
count++;
}
}
cout << "Alphabets = " << count;
return 0;
}
Alphabets = 5
Counting Vowels Using Character Functions
String और character functions को combine करके vowels की संख्या भी count की जा सकती है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text = "Education";
int count = 0;
for (int i = 0; i < text.length(); i++)
{
char ch = tolower(text[i]);
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u')
{
count++;
}
}
cout << "Vowels = " << count;
return 0;
}
Vowels = 5
String and Character Functions Together
Real-world programs में string और character functions का उपयोग अक्सर एक साथ किया जाता है। उदाहरण के लिए, किसी password या user input में alphabets, digits और special characters की जाँच की जा सकती है।
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text = "ABC123";
int letters = 0;
int digits = 0;
for (int i = 0; i < text.length(); i++)
{
if (isalpha(text[i]))
{
letters++;
}
else if (isdigit(text[i]))
{
digits++;
}
}
cout << "Letters = " << letters << endl;
cout << "Digits = " << digits;
return 0;
}
Letters = 3
Digits = 3
Important Points
length()औरsize()string की length प्राप्त करते हैं।append()string के अंत में दूसरी string जोड़ता है।find()character या substring की position search करता है।substr()string का एक हिस्सा प्राप्त करता है।insert()string में characters या substring जोड़ता है।erase()string के characters को हटाता है।replace()string के किसी भाग को दूसरी string से बदलता है।isalpha()alphabet check करता है।isdigit()digit check करता है।isalnum()alphabet या digit check करता है।islower()lowercase character check करता है।isupper()uppercase character check करता है।isspace()whitespace character check करता है।tolower()character को lowercase में convert करता है।toupper()character को uppercase में convert करता है।
Board Focus
length() → String की length
size() → String की length
append() → String जोड़ना
find() → Search करना
substr() → String का भाग निकालना
insert() → Characters insert करना
erase() → Characters हटाना
replace() → String का भाग बदलना
isalpha() → Alphabet check
isdigit() → Digit check
isalnum() → Alphabet/Digit check
islower() → Lowercase check
isupper() → Uppercase check
isspace() → Whitespace check
tolower() → Lowercase conversion
toupper() → Uppercase conversion
Board Important Questions
Very Short Answer Questions
Q1. length() function का क्या उपयोग है?
Answer: length() function string में मौजूद characters की संख्या प्राप्त करने के लिए उपयोग किया जाता है।
Q2. find() function क्या करता है?
Answer: find() function string में किसी character या substring की position खोजता है।
Q3. substr() function का क्या उपयोग है?
Answer: substr() function string के किसी particular part को प्राप्त करने के लिए उपयोग किया जाता है।
Q4. isalpha() function क्या check करता है?
Answer: isalpha() check करता है कि दिया गया character alphabet है या नहीं।
Q5. isdigit() function क्या check करता है?
Answer: isdigit() check करता है कि दिया गया character digit है या नहीं।
Q6. toupper() का क्या उपयोग है?
Answer: toupper() lowercase alphabetic character को uppercase में convert करने के लिए उपयोग किया जाता है।
Q7. tolower() का क्या उपयोग है?
Answer: tolower() uppercase alphabetic character को lowercase में convert करने के लिए उपयोग किया जाता है।
Short Answer Questions
Q8. String functions और character functions में अंतर बताइए।
| String Functions | Character Functions |
|---|---|
| पूरी string पर operations के लिए उपयोग होते हैं। | Individual characters को check या convert करने के लिए उपयोग होते हैं। |
उदाहरण: length(), find(), substr() |
उदाहरण: isalpha(), isdigit(), toupper() |
Q9. isalpha(), isdigit() और isalnum() में अंतर बताइए।
| Function | Purpose |
|---|---|
isalpha() |
Alphabet check करता है। |
isdigit() |
Digit check करता है। |
isalnum() |
Alphabet या digit check करता है। |
Q10. length() और size() में क्या अंतर है?
Answer: सामान्य string usage में length() और size() दोनों string में मौजूद characters की संख्या return करते हैं। इसलिए string की length प्राप्त करने के लिए दोनों का उपयोग किया जा सकता है।
Q11. find() और substr() functions को उदाहरण सहित समझाइए।
Answer: find() string में किसी character या substring की position खोजता है, जबकि substr() string के किसी भाग को प्राप्त करता है।
string word = "Computer";
cout << word.find("put");
cout << word.substr(0, 4);
3Comp
Long Answer Questions
Q12. C++ के प्रमुख string functions को उदाहरण सहित समझाइए।
Answer: C++ में string class कई useful functions प्रदान करती है। length() और size() string की length प्राप्त करते हैं। append() दूसरी string को जोड़ता है। find() किसी character या substring को search करता है। substr() string का एक भाग प्राप्त करता है। insert() characters को किसी position पर जोड़ता है, erase() characters हटाता है और replace() string के किसी भाग को बदलता है।
Q13. C++ के प्रमुख character functions को उदाहरण सहित समझाइए।
Answer: C++ में <cctype> header file के माध्यम से कई character functions उपलब्ध हैं। isalpha() alphabet check करता है, isdigit() digit check करता है, isalnum() alphabet या digit check करता है, islower() और isupper() case check करते हैं। tolower() और toupper() characters को lowercase और uppercase में convert करने के लिए उपयोग किए जाते हैं।
Q14. निम्न program का output बताइए:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Programming";
cout << word.length() << endl;
cout << word.substr(0, 7);
return 0;
}
11
Program
Q15. निम्न program का output बताइए:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char a = 'A';
char b = '5';
cout << (char)tolower(a) << endl;
cout << isdigit(b);
return 0;
}
a
1
Quick Revision
- length() / size(): String की length
- append(): String जोड़ना
- find(): Search करना
- substr(): String का भाग प्राप्त करना
- insert(): Characters insert करना
- erase(): Characters हटाना
- replace(): String का भाग बदलना
- isalpha(): Alphabet check
- isdigit(): Digit check
- isalnum(): Alphabet/Digit check
- islower(): Lowercase check
- isupper(): Uppercase check
- isspace(): Whitespace check
- tolower(): Lowercase conversion
- toupper(): Uppercase conversion
Practice Questions
- String functions क्या हैं?
- Character functions क्या हैं?
length()औरsize()का उपयोग लिखिए।append()function का syntax और example लिखिए।find()function का क्या उपयोग है?substr()function को उदाहरण सहित समझाइए।insert(),erase()औरreplace()functions का उपयोग लिखिए।isalpha(),isdigit()औरisalnum()में अंतर बताइए।islower()औरisupper()का उपयोग लिखिए।tolower()औरtoupper()functions को उदाहरण सहित समझाइए।- एक C++ program लिखिए जो string में alphabets और digits की संख्या count करे।
- एक C++ program लिखिए जो string को uppercase में convert करे।
- एक C++ program लिखिए जो string को lowercase में convert करे।
- एक C++ program लिखिए जो string में vowels की संख्या count करे।
- निम्न program का output बताइए:
string s = "Hello"; cout << s.length() << endl; cout << s.find('l'); - निम्न program का output बताइए:
char ch = 'a'; cout << (char)toupper(ch);