
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to check if first and the last characters of string are equal
Given with an input of string and the task is to check whether the first and last characters of given string is equal or not.
Example
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’
Approach used below is as follows −
- Input the string and store in an array of strings.
- Calculate the length of a string using length() function
- Check first and last element of a string array, if they are equal return 1 else retrun -1
- Print the resultant output
Algorithm
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
Example
#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"; }
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
yes it have first and last equal characters
- Related Questions & Answers
- Java Program to check if two dates are equal
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- Java Program to check if the String contains only certain characters
- Java program to swap first and last characters of words in a sentence
- Python - Check if frequencies of all characters of a string are different
- Count substrings with same first and last characters in C++
- Python program to check if both halves of the string have same set of characters.
- Check if two SortedSet objects are equal in C#
- Check if two BitArray objects are equal in C#
- Check if two ArrayList objects are equal in C#
- Check if two HashSet objects are equal in C#
- Check if two HybridDictionary objects are equal in C#
- Check if two LinkedList objects are equal in C#
- Check if two OrderedDictionary objects are equal in C#
- Check if two SortedList objects are equal in C#
Advertisements