Program to print smallest and biggest possible palindrome word in a given string

In this program, we need to find the smallest and biggest palindromic word present in the given string.

Wow you own kayak

In above example, wow represent the smallest palindrome and kayak represent the biggest palindrome. One of the approach to accomplish this task is split the string into word. Then, check whether the words are palindrome or not. Then, compare their length to find out the minimum and maximum palindromic word.

Algorithm

main()

  • STEP 1: START
  • STEP 2: DEFINE String string = “Wow you own kayak “
  • STEP 3: DEFINE word = ” “, smallPalin = ” “, bigPalin = ” “
  • STEP 4: DEFINE String words[]
  • STEP 5: SET temp = 0, count = 0
  • STEP 6: CONVERT the string into lowercase
  • STEP 7: string = string + ” “
  • STEP 8: SET i=0. REPEAT STEP 9 to STEP 11 UNTIL i<string.length()
  • STEP 9: SPLIT the string into words.
  • STEP 10: IF(string.charAt(i) != ‘ ‘) then
    word = word + string.charAt(i)
    else
    words[temp]= word
    temp = temp+1
    word = ” “
  • STEP 11: i=i+1
  • STEP 12: SET i=0. REPEAT STEP 13 to STEP 17 UNTIL i<temp
  • STEP 13: IF( isPalindrome(words[i]) ) then
    count = count + 1
    goto STEP 14
  • STEP 14: IF(count==1)
  • STEP 15: IF length of smallPalin is greater than the length of words[i] then
    smallPalin = words[i]
  • STEP 16: IF length of bigPalin is lesser than the length of words[i] then
    bigPalin = words[i]
  • STEP 17: i=i+1
  • STEP 18: IF(count==0) then PRINT “No palindrome is present in the given string “
    else
    PRINT smallPalin, bigPalin
  • STEP 19: END

isPalindrome(String a)


Important Interview Questions answer

Program:

  1. import java.io.BufferedReader;  
  2. import java.io.FileReader;  
  3.   
  4. public class CountWordFile  
  5. {  
  6.     public static void main(String[] args) throws Exception {  
  7.         String line;  
  8.         int count = 0;  
  9.   
  10.         //Opens a file in read mode  
  11.         FileReader file = new FileReader(“data.txt “);  
  12.         BufferedReader br = new BufferedReader(file);  
  13.   
  14.         //Gets each line till end of file is reached  
  15.         while((line = br.readLine()) != null) {  
  16.             //Splits each line into words  
  17.             String words[] = line.split(“”);  
  18.             //Counts each word  
  19.             count = count + words.length;  
  20.         }  
  21.   
  22.         System.out.println(“Number of words present in given file: ” + count);  
  23.         br.close();  
  24.     }  
  25. }  

Output:

Number of words present in given file: 63

Important Interview Questions answer

Free Courses Enroll Today

Leave a Comment