- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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 program to check if a string contains all unique characters
- Python program to check if both halves of the string have same set of characters.
- Java Program to check if two dates are equal
- Count substrings with same first and last characters in C++
- Python - Check If All the Characters in a String Are Alphanumeric?
- Java program to check order of characters in string
- Python Program to check if String contains only Defined Characters using Regex
- Python - Check if frequencies of all characters of a string are different
- Java Program to check if the String contains any character in the given set of characters
- C# Program to check a string for whitespace characters or null
- Check if the characters of a given string are in alphabetical order in Python
- Check if both halves of the string have same set of characters in C#

Advertisements