একটি স্ট্রিং ইনপুট দেওয়া থাকলে, স্ট্রিং-এর প্রথম ও শেষ ক্যারেক্টার সমান কিনা তা যাচাই করতে হবে।
উদাহরণ
Input: study Output: not equal - কারণ শুরুতে 's' এবং শেষে 'y' Input: nitin Output: yes it have first and last equal characters - কারণ শুরু ও শেষে 'n'
ব্যবহৃত পদ্ধতি:
- স্ট্রিং ইনপুট নিয়ে স্ট্রিং অ্যারেতে সংরক্ষণ করুন
- length() ফাংশন দিয়ে স্ট্রিং-এর দৈর্ঘ্য বের করুন
- স্ট্রিং অ্যারের প্রথম ও শেষ উপাদান চেক করুন, সমান হলে ১ ফেরত দিন অন্যথায় -১
- ফলাফল প্রিন্ট করুন
আলগরিদম
Start
Step 1 -> declare function to check if first and last characters 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
C++ প্রোগ্রাম উদাহরণ
#include<iostream>
using namespace std;
//function to check if first and last characters 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";
return 0;
}
আউটপুট
yes it have first and last equal characters