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

C++-এ strchr() ফাংশন এবং এর অ্যাপ্লিকেশন

এই লেখায় আমরা C++ STL-এ strchr() ফাংশনের কাজ করার পদ্ধতি, সিনট্যাক্স এবং উদাহরণ নিয়ে আলোচনা করব।

strchr() কী?

strchr() হল C++ STL-এ একটি বিল্ট-ইন ফাংশন, যা <cstring> হেডার ফাইলে সংজ্ঞায়িত। strchr() ফাংশনটি একটি স্ট্রিং-এ ক্যারেক্টারটি প্রথম কখন આવেছে তা খুঁজে বের করতে ব্যবহৃত হয়। এই ফাংশনটি সেই লোকেশনের পয়েন্টার রিটার্ন করে যেখানে ক্যারেক্টারটি প্রথমবার স্ট্রিং-এ দেখায়।

যদি ক্যারেক্টারটি স্ট্রিং-এ না থাকে তবে ফাংশনটি নাল পয়েন্টার রিটার্ন করে।

সিনট্যাক্স

char* strchr( char* str, char charac );

প্যারামিটার

  • str — এটি वह স্ট্রিং যেখানে আমরা ক্যারেক্টারটি খুঁজব।
  • charac — এটি वह ক্যারেক্টার যা আমরা স্ট্রিং str-এ সার্চ করতে চাই।

রিটার্ন ভ্যালু

এই ফাংশন ক্যারেক্টারটি প্রথমবার স্ট্রিং-এ যেখানে দেখায় সেই লোকেশনের পয়েন্টার রিটার্ন করে। যদি ক্যারেক্টারটি না পাওয়া যায় তবে নাল পয়েন্টার রিটার্ন করে।

উদাহরণ ১

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str[] = "Tutorials Point";
    char ch_1 = 'b', ch_2 = 'T';

    if (strchr(str, ch_1) != NULL)
        cout << ch_1 << " " << "is present in string" << endl;
    else
        cout << ch_1 << " " << "is not present in string" << endl;

    if (strchr(str, ch_2) != NULL)
        cout << ch_2 << " " << "is present in string" << endl;
    else
        cout << ch_2 << " " << "is not present in string" << endl;

    return 0;
}

আউটপুট

b is not present in string
T is present in string

উদাহরণ ২: স্ট্রিং কনক্যাটেনেশন সহ

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str[] = "Tutorials Point";
    char str_2[] = " is a learning portal";
    char ch_1 = 'b', ch_2 = 'T';

    if (strchr(str, ch_1) != NULL) {
        cout << ch_1 << " " << "is present in string" << endl;
    } else {
        cout << ch_1 << " " << "is not present in string" << endl;
    }

    if (strchr(str, ch_2) != NULL) {
        cout << ch_2 << " " << "is present in string" << endl;
        strcat(str, str_2);
        cout << "String after concatenation is: " << str;
    } else {
        cout << ch_2 << " " << "is not present in string" << endl;
    }

    return 0;
}

আউটপুট

b is not present in string
T is present in string
String after concatenation is: Tutorials Point is a learning portal
  1. C++-এ স্ট্রিংকে নম্বরে এবং নম্বরকে স্ট্রিংএ কনভার্ট করা

  2. C++-এ পিউর ফাংশন

  3. C++-এ strchr() ফাংশন

  4. C++-এ ফ্রেন্ড ক্লাস এবং ফাংশন