Program to check if first and the last characters of string are equal
We are given a string, we need check whether the first and last characters of the string str are equal or not. Case sensitivity is to be considered.
Examples :
Input : university Output : Not Equal Explanation: In the string "university", the first character is 'u' and the last character is 'y', as they are not equal, "Not Equal" is the output. Input : racecar Output : Equal Explanation: In the string "racecar", the first character is 'r' and the last character is 'r', as they are equal, "Equal" is the output.
C++
// C++ program to check if the first // and the last characters of a string // are equal or not. #include<iostream> using namespace std; // Function to check if the first // and the last haracters of a // string are equal or not. int areCornerEqual(string s) { int n = s.length(); if (n < 2) return -1; if (s[0] == s[n - 1]) return 1; else return 0; } // Driver code int main() { string s = "GfG"; int res = areCornerEqual(s); if (res == -1) cout<<"Invalid Input"; else if (res == 1) cout<<"Equal"; else cout<<"Not Equal"; } // This code is contributed by // Smitha Dinesh Semwal |
chevron_right
filter_none
Java
// Java program to check if the first and the last // characters of a string are equal or not. class GFG { public static int areCornerEqual(String s) { int n = s.length(); if (n < 2) return -1; if (s.charAt(0) == s.charAt(n-1)) return 1; else return 0; } // Driver code public static void main(String[] args) { String s = "GfG"; int res = areCornerEqual(s); if (res == -1) System.out.println("Invalid Input"); else if (res == 1) System.out.println("Equal"); else System.out.println("Not Equal"); } } |
chevron_right
filter_none
Python3
# Python program to check # if the first and the # last characters of a # string are equal or not. st = "GfG"if(st[0] == st[-1]): # print output # if condition # is satisfied print("Equal") else: # print output # if condition is # not satisfied print("Not Equal") # This code is contributed by # shivi Aggarwal |
chevron_right
filter_none
C#
// Java program to check if the first and the last // characters of a string are equal or not. using System; class GFG { public static int areCornerEqual(String s) { int n = s.Length; if (n < 2) return -1; if (s[0] == s[n - 1]) return 1; else return 0; } // Driver code static public void Main () { String s = "GfG"; int res = areCornerEqual(s); if (res == -1) Console.WriteLine("Invalid Input"); else if (res == 1) Console.WriteLine("Equal"); else Console.WriteLine("Not Equal"); } } // This code is contributed by Ajit. . |
chevron_right
filter_none
PHP
<?php // PHP program to check if // the first and the last // characters of a $are // equal or not. function areCornerEqual($s) { $n = strlen($s); if ($n < 2) return -1; if ($s[0] == $s[$n - 1]) return 1; else return 0; } // Driver code $s = "GfG"; $res = areCornerEqual($s); if ($res == -1) echo ("Invalid Input"); else if ($res == 1) echo ("Equal"); else echo ("Not Equal"); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
chevron_right
filter_none
Output:
Equal
Recommended Posts:
- Python program to check if a string contains all unique characters
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Check whether second string can be formed from characters of first string
- Check if both halves of the string have same set of characters
- Quick way to check if all the characters of a string are same
- Check whether the frequencies of all the characters in a string are prime or not
- Check if both halves of the string have same set of characters in Python
- Check if the characters of a given string are in alphabetical order
- Check if characters of one string can be swapped to form other
- Check if a given string is made up of two alternating characters
- Program to check if all characters have even frequency
- Check if a string has all characters with same frequency with one variation allowed
- Check if string follows order of characters defined by a pattern or not | Set 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


