Program to check if first and the last characters of string are equal - GeeksforGeeks

04 May, 2021
We are given a string, we need to 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++
Java
Python3
C#
PHP
Javascript
// 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 codeint 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
// 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");    }}
# 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
// 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..
<?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)?>
<script>     // Javascript program to check    // if the first and the last    // characters of a string are equal or not.         function areCornerEqual(s)    {        let n = s.length;        if (n < 2)            return -1;        if (s[0] == s[n - 1])            return 1;        else            return 0;    }         let s = "GfG";    let res = areCornerEqual(s);     if (res == -1)      document.write("Invalid Input");    else if (res == 1)      document.write("Equal");    else      document.write("Not Equal");         </script>
Output:  
Equal
 
YouTubeGeeksforGeeks500K subscribersProgram to check if first and the last characters of string are equal | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:19•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=77rW4tCB56c" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
Smitha Dinesh Semwal
jit_t
manishshaw1
Shivi_Aggarwal
suresh07
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Ternary Operator in Python
Destructors in C++
Difference between Abstract Class and Interface in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching