Tuesday, October 18, 2022

BlueJ Program On Mixing Two Words With Alternate Characters

 In this program, two words will be taken from user and the words will be combined to form a single word in the following manner. 1st letter of the first word will be followed by the 1st letter of the second word and so on. If the words are of different length, the remaining characters of the longest word is placed at the end.

Example: 1st word: JUMP
                 2nd word: STROLL
Output: JSUTMRPOLL
import java.util.*;

public class WordMix
{
String wrd;
int len;
Scanner sc=new Scanner(System.in);
WordMix()
{
    wrd="";
    len=0;
}
void feedWord()
{
    System.out.print("\nEnter the word: ");
    wrd=sc.next().toUpperCase();
    len=wrd.length();
}
void mixWord(Main ob1, Main ob2)
{
    int i,j;
    i=0;
    j=0;
    wrd="";
    while(i<ob1.len && j<ob2.len)
    {
        wrd=wrd+ob1.wrd.charAt(i)+ob2.wrd.charAt(j);
        i++;
        j++;
    }
    if(i==ob1.len)
            wrd=wrd+ob2.wrd.substring(j+1);
     else
           wrd=wrd+ob1.wrd.substring(i+1);
     System.out.print("\nFinal Word :"+wrd)   ;
    }
   public static void main(String args[])
   {
      WordMix ob1=new WordMix();
      WordMix ob2=new WordMix();
       WordMix ob3=new WordMix();
       ob1.feedWord();
       ob2.feedWord();
       ob3.mixWord(ob1,ob2);
   }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner