Write a java program to Check Characters in a String

Write a program to read a string and to test whether the first and last character is same. The string is said to be valid if the 1st and last character are the same. Else the string is said to be invalid.

Input and Output Format:

Input consists of a string.

The output is a string saying characters are same or not.

Refer sample output for formatting specifications.

Sample Input 1:

the picture was great

Sample Output 1:
Sample Input 1:


Sample Output 1:


Check Characters in a String in Java.

 import java.util.Scanner;
import java.util.StringTokenizer;
public class Main
                public static void main(String[] args)
                	Scanner sc = new Scanner(System.in);
                	String s1 = sc.nextLine();
                    int n =  checkForFirstAndLastChar(s1);
                public static int checkForFirstAndLastChar(String input)
                                StringTokenizer t = new StringTokenizer(input," ");
                                String s = t.nextToken();
                                String s1 = " " ;
                                                s1 = t.nextToken();
                                if(s.charAt(0) == s1.charAt(s1.length()-1))
                                                return 1;
                                                return 0;

Add a Comment

Your email address will not be published. Required fields are marked *

two × five =