06A-2016 – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2016-06A #JAVA#7321    siteicon   siteicon  

Problem Statement - 06A-2016

Special words are those words which starts and ends with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice-versa.
Example:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes.
Write a program to accept a word check and print whether the word is a palindrome or only special word.

Solution

TC++ #7321

import java.util.Scanner;

 public class Words {

 public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter word: ");
 String word = scanner.next();

 // Check if word is palindrome
 String reverse = "";
 for (int i = word.length() - 1; i >= 0; i--) {
 reverse = reverse + word.charAt(i);
 }
 if (word.equals(reverse)) {
 System.out.println("Palindrome");
 }

 // Check if word is a special word
 char firstLetter = word.charAt(0);
 char lastLetter = word.charAt(word.length() - 1);
 if (firstLetter == lastLetter) {
 System.out.println("Special word");
 }
 }
 }

 

Sample output

Enter word: MALAYALAM
Palindrome
Special word


Share

CSKC| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016









Back