Q:

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

belongs to collection: Java String Programs

0

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)
    • smallPalin = bigPalin = words[i]

 

    else go to STEP 15 and STEP 16
  • 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)

      STEP 1: START STEP 2: SET flag = true STEP 3: SET i=0. REPEAT STEP 4 to STEP 5 UNTIL i<a.length()/2 STEP 4: IF(a.charAt(i) != a.charAt(a.length()-i-1) then

 

      flag = false

 

    break STEP 5: i=i+1 STEP 6: RETURN flag STEP 7: END

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

import java.io.BufferedReader;  
import java.io.FileReader;  
  
public class CountWordFile  
{  
    public static void main(String[] args) throws Exception {  
        String line;  
        int count = 0;  
  
        //Opens a file in read mode  
        FileReader file = new FileReader("data.txt ");  
        BufferedReader br = new BufferedReader(file);  
  
        //Gets each line till end of file is reached  
        while((line = br.readLine()) != null) {  
            //Splits each line into words  
            String words[] = line.split("");  
            //Counts each word  
            count = count + words.length;  
        }  
  
        System.out.println("Number of words present in given file: " + count);  
        br.close();  
    }  
}  

Output:

Number of words present in given file: 63

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Reverse a String in Java Word by Word... >>
<< Java Program to swap two string variables without ...