স্ট্রিং এর একটি ইনপুট দিয়ে দেওয়া হয়েছে এবং কাজ হল প্রদত্ত স্ট্রিং এর প্রথম এবং শেষ অক্ষর সমান কিনা তা পরীক্ষা করা।
উদাহরণ
Input-: study Output-: not equal As the starting character is ‘s’ and the end character of a string is ‘y’ Input-: nitin Output-: yes it have first and last equal characters As the starting character is ‘n’ and the end character of a string is ‘n’
নিচে ব্যবহৃত পদ্ধতিটি নিম্নরূপ −
- স্ট্রিংটি ইনপুট করুন এবং স্ট্রিংগুলির একটি অ্যারেতে সংরক্ষণ করুন।
- দৈর্ঘ্য() ফাংশন ব্যবহার করে একটি স্ট্রিংয়ের দৈর্ঘ্য গণনা করুন
- একটি স্ট্রিং অ্যারের প্রথম এবং শেষ উপাদান চেক করুন, যদি তারা সমান রিটার্ন 1 অন্য রিটার্ন -1 হয়
- ফলাফল আউটপুট প্রিন্ট করুন
অ্যালগরিদম
Start Step 1-> declare function to check if first and last charcters are equal or not int check(string str) set int len = str.length() IF (len < 2) return -1 End If (str[0] == str[len - 1]) return 1 End Else return 0 End Step 2->Int main() declare string str = "tutorialsPoint" set int temp = check(str) If (temp == -1) Print “enter valid input" End Else if (temp == 1) Print "yes it have first and last equal characters" End Else Print "Not equal” Stop
উদাহরণ
#include<iostream>
using namespace std;
//function to check if first and last charcters are equal or not
int check(string str) {
int len = str.length();
if (len < 2)
return -1;
if (str[0] == str[len - 1])
return 1;
else
return 0;
}
int main() {
string str = "tutorialsPoint";
int temp = check(str);
if (temp == -1)
cout<<"enter valid input";
else if (temp == 1)
cout<<"yes it have first and last equal characters";
else
cout<<"Not equal";
} আউটপুট
যদি আমরা উপরের কোডটি চালাই তবে এটি নিম্নলিখিত আউটপুট তৈরি করবে
yes it have first and last equal characters