স্ট্রিংয়ের দৈর্ঘ্য হলো NULL ক্যারেক্টার ('\0') এর আগ পর্যন্ত ক্যারেক্টার সংখ্যা।
১. ম্যানুয়াল লুপ দিয়ে
#include <iostream>
using namespace std;
int main() {
char str[] = "Apple";
int count = 0;
while (str[count] != '\0')
count++;
cout << "The string is " << str << endl;
cout << "The length of the string is " << count << endl;
return 0;
}
আউটপুট
The string is Apple The length of the string is 5
২. strlen() ফাংশন দিয়ে
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Grapes are green";
cout << "The string is " << str << endl;
cout << "The length of the string is " << strlen(str);
return 0;
}
আউটপুট
The string is Grapes are green The length of the string is 16
ব্যাখ্যা
- ম্যানুয়াল:
while (str[count] != '\0') count++;— NULL পাওয়া পর্যন্ত কাউন্ট। - strlen():
<cstring>হেডারে। NULL ছাড়া ক্যারেক্টার সংখ্যা রিটার্ন করে। - C++ string ক্লাস:
std::string s = "text"; s.length()বাs.size()— আরও সুবিধাজনক।