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

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

by Online Tutorials Library

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)
  • 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

Program:

Output:

Number of words present in given file: 63  
Next TopicJava Programs

You may also like