Top 5 java String Programming and Coding Interview Questions with Solutions


Java Program to find the largest & smallest word in a string

 In the following java program, we have taken one sentence in str variable and extract the word and find its length and then compare to the max = -999, if it is greater than the max then assign max to word length and also store the word to longWord variable longWord = word;

 

import java.util.Scanner;

public class LongestWordinASentence

{

    public static void main(String args[])

    {

        int len;        // length of the string

        int i;          // for loop

        int max=-999;   // for maximum noumber

        int wlen;       // individual word length

        char ch;        // for character

        String word = "";       // storing words

        String longWord = "" ;  // storing longest word

        Scanner in = new Scanner(System.in);

        System.out.println("Enter a word or sentence:");

        String str = in.nextLine();

        //Add space at end of string

        str=str + " ";

        // length of the sentence is stored in len

        len = str.length();

        for(i=0; i<len; i++)

        {

            ch=str.charAt(i);

            // add character in word till space encounter

            if(ch != ' ')

            {

                word= word + ch;

            }

            else

            {

                wlen=word.length();

                // check word length with max=-999 first

                // if maximum assign max=word length

                // assign the word to longWord string variable

                if(wlen>max)

                {                  

                    max=wlen;

                    longWord=word;                                    

                }

                // initialize the word

                word="";

            }

        }

        // print the longest word with it's length

        System.out.println("The longest word is " + longWord + " has length " + max);

    }

}// end of the program

Output:

Enter a word or sentence:

everybody should come to the party

The longest word is everybody has length 9

 

java program to replace a given word with another in a string

In the following, we have replaced all words ‘red’ with ‘blue’ in the given sentence. In this program extract the word and check each word with ‘red’, if ‘red’ then change to ‘blue’.

 

public class ReplaceWord

{

    public static void main(String args[])

    {

        // sentence taken in st variable

        String st="the red bottle contain red water keep on a red table";

        String word=""; // word taken from the sentence

        String newSt="";// New sentence form with blue word istead of red

        char ch;

        int l, i;

        st=st + " ";

        l=st.length();

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // extract word

            if(ch!=' ')

            {

                word=word+ch;

            }

            else

            {

                // checking word red, then change to blue

                if(word.equals("red"))

                {

                    word="blue"; // assign blue

                    newSt=newSt + word + " ";

                    word="";

                }

                newSt=newSt + word + " ";

                word="";

            }

        }

        System.out.println(st);

        System.out.println(newSt);

    }

}

Output:

the red bottle contains red water keep on a red table

the blue  bottle contains blue  water keep on a blue  table

 

Java program to print vowel of each word of a sentence

This program takes one sentence and extracts each word and counts its vowels and print each word with their respective vowels.

import java.util.*;

public class EachWordsVowelPrint

{

    public static void main(String args[])

    {

        String st, word="";

        char ch,ch1;

        int v;      // variable for vowel

        int c;      // variable for Consonants

        int w;      // variable for word

        int i,j;    // for loop

        int l,l1;   // l is for length of sentence and for word

        v=c=w=0;    // Initialization

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence:");

        st=sc.nextLine();

        st=st + " ";    // one space add to the sentence

        //st=st.toUpperCase();

        l=st.length();  // length of the sentence

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

            else

            {

                l1=((word.trim()).length());   // length of the word               

                for(j=0; j<l1; j++)

                {

                    ch1=word.charAt(j);                    

                    switch(ch1)

                    {

                        case 'A' :

                        case 'a' :

                        case 'E' :

                        case 'e' :

                        case 'I' :

                        case 'i' :

                        case 'O' :

                        case 'o' :

                        case 'U' :

                        case 'u' :

                        v++;

 

                    }                 

                }

                // print the word total vowels

                System.out.println(word + " contain vowel= " + v);

                // Initialize the words and v for vowel

                word = "";   

                v=0;

            }

        }

    }

}

Output:

Enter a sentence:

education is the best policy

education contain vowel= 5

is contain vowel= 1

the contain vowel= 1

best contain vowel= 1

policy contain vowel= 2

 

java program to capitalize each word in string

 

public class CapitalizeEachWord

{

    public static void main(String args[])

    {

        String st="the quick brown dog jumps over a lazy hungary fox";

        String newSt =""; // each word capitalize the it form a sentence

        String word ="";  // word taken from the sentence  

        String newWord;  // new word with first letter capital

        char ch;

        int l, i;

        st=st + " ";

        l=st.length();

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            if(ch!=' ')

            {

                word=word+ch;

            }

            else

            {

                // convert word first letter capital

                newWord=Character.toUpperCase(word.charAt(0)) + word.substring(1);

                newSt=newSt + newWord + " ";

                word="";

            }

        }

        System.out.println(st);

        System.out.println(newSt);

    }

}

 

Output:

the quick brown dog jumps over a lazy hungry fox

The Quick Brown Dog Jumps Over A Lazy Hungry Fox

 

java program to print vowel, words and consonant in string

 

public class TotalVowelConsonantWords

{

    public static void main(String args[])

    {

        String st="Education is must for all";

        char ch;

        int i, l, c,sp, con,v1, v2, v3, v4, v5;

        // Initialisation of variables

        v1 = v2 = v3 = v4 = v5 = c = sp = 0;

        l=st.length();

        for(i=0;i<l;i++)

        {

            ch=st.charAt(i);

            switch(ch)

            {

                case 'A':

                case 'a':

                v1++;       // vowel a count

                break;

                case 'E':

                case 'e':

                v2++;       // vowel e count

                break;

                case 'I':

                case 'i':

                v3++;       // vowel i count

                break;

                case 'O':

                case 'o':

                v4++;       // vowel o count

                break;

                case 'U':

                case 'u':

                v5++;       // vowel u count

                break;

            }

            // Calculate total Space for count words

            if(ch==' ')

                sp++;

        }

        // consonants = length of String - (total vowels + space)

        System.out.println("Sentence " + st);

        con = l - (v1 + v2 + v3 + v4 + v5 + sp);

        System.out.println("Total String Length = " + l);

        System.out.println("Total Vowel A= " +v1);

        System.out.println("Total Vowel E= " +v2);

        System.out.println("Total Vowel I= " +v3);

        System.out.println("Total Vowel O= " +v4);

        System.out.println("Total Vowel U= " +v5);

        System.out.println("Total Words = " +(sp+1)); // total space plus 1

        System.out.println("Total Consonants = "+ con);

    }

}

Output:

Sentence Education is must for all

Total String Length = 25

Total Vowel A= 2

Total Vowel E= 1

Total Vowel I= 2

Total Vowel O= 2

Total Vowel U= 2

Total Words = 5

Total Consonants = 12



More Java programs 




SHARE THIS
Previous Post
Next Post