কম্পিউটার টিউটোরিয়াল

স্ট্রিংয়ের দৈর্ঘ্য (Length) বের করার C++ প্রোগ্রাম


স্ট্রিংয়ের দৈর্ঘ্য হলো 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() — আরও সুবিধাজনক।
  1. C++ দিয়ে একটি স্ট্রিংয়ের সাবস্ট্রিং-এর সংখ্যা বের করা

  2. সি++-এ একটি রেখার মিড-পয়েন্ট বের করার প্রোগ্রাম

  3. সি++-এ ত্রিভুজের কেন্ড্রয়েড (Centroid) বের করার প্রোগ্রাম

  4. সি++-এ সমান্তরাল চতুর্ভুজের (Parallelogram) ক্ষেত্রফল বের করার প্রোগ্রাম