Showing posts with label Java String Programs. Show all posts
Showing posts with label Java String Programs. Show all posts

Tuesday, January 7, 2025

BlueJ Program To Display Names With Surname in Alphabetical Order of Surnames

 
import java.util.*;
class S
{   
 String name[], surname[];
 int i,j,n;
 String s;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
         System.out.print("\nEnter the No of persons:");
         n=sc.nextInt();
         name=new String[n];
         surname=new String[n];         
         for(i=0;i<n;i++)
         {
             System.out.print("\nName:");
             name[i]=sc.next().toUpperCase();
             System.out.print("\nSurname:");
             surname[i]=sc.next().toUpperCase();
            }
                for(i=0;i<n;i++)
                {
                     for(j=i+1;j<n;j++)
                     {
                         if(surname[i].compareTo(surname[j])>0)
                         {
                             s=surname[i];
                             surname[i]=surname[j];
                             surname[j]=s;
                             s=name[i];
                             name[i]=name[j];
                             name[j]=s;
                            }
                        }
                    }
                System.out.println("Name with Surname in alphabetical order of surname");
                for(i=0;i<n;i++)
                System.out.println(name[i]+ " "+surname[i]);
                               
             }
            }
        
    

Monday, December 23, 2024

BlueJ Program Eliminating Duplicate Letters From A Senrence

 import java.util.*;
class S
{   
 String str,str1;
    int i,j,len,c;
    char ch[],ch1;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
       str1="";
       c=0;
       System.out.print("\nEnter the sentence:");
      str=sc.nextLine().toLowerCase();
      len=str.length();
      ch=new char[len];
      System.out.print("\nEntered sentence:"+str);
      for(i=0;i<len;i++)
      {
         ch1=str.charAt(i);
         for(j=0;j<c;j++)
         {
           if( str1.charAt(j)==ch1)           
           break;
         }
         if(j==c)
         {
         str1=str1+ch1;
         ch[c++]=ch1;
        }
    }
     
     System.out.print("\nSentence elimination of repeated letters:"+str1);     
    
       } 
    }
         

Monday, December 16, 2024

BlueJ Program To Display Words of A Sentence In Alphabetical Order

 import java.util.*;
class Sen
{   
 String arr[]=new String[15];
    String str,t;
    int i,j,c;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
       System.out.print("\nEnter the sentence:");
      str=sc.nextLine()+" ";
      c=0;
      while(true)
      {
          i=str.indexOf(' ');
          if(i<0)
          break;
          arr[c++]=str.substring(0,i);
          str=str.substring(i+1);
      }
       
     for(i=0;i<c-1;i++)
     {
         for(j=i+1;j<c;j++)
     {
              if(arr[i].compareToIgnoreCase(arr[j])>0)
              {
                  t=arr[i];
                  arr[i]=arr[j];
                  arr[j]=t;
              }
     }
    }
     
     System.out.print("\nWords in alphabetical order\n");
     for(i=0;i<c;i++)
     {
         
        System.out.print(" "+arr[i]);
    }
    
       } 
    }
         

Sunday, October 1, 2023

Converting Non-Palindrome Word Into Palindrome

 Convert the non-palindrome words of the sentence into palindrome words by concatenating the word by its reverse (excluding the last character).

Example: The reverse word of HELP would be LEH (omitting the last alphabet) and by concatenating both, the new palindrome word is HELPLEH. Thus the word HELP becomes HELPLEH.

Note: The words which end with repeated alphabets, for example ABB would become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.


import java.util.*;

class Ab

{

  String str1,str2;

  Scanner sc=new Scanner(System.in);

  int c=0;

  void show()

  {

      int len,i;

      c=1;

      System.out.print("\nEnter the Word:");

      str1=sc.next();

      if(notPalin(str1))

      {

         len=str1.length();

         for(i=len-1;i>0;i--)

         {

             if(str1.charAt(i)==str1.charAt(i-1))

             c++; /* 'c' marks the index where mismatch of characters happen */

            }

         }

         str2=leftRev(str1.substring(0,str1.length()-c)); 

         str1=str1+str2;

         System.out.println("\nModified="+str1);

      }

    

    private boolean notPalin(String str1)

    {

       int i,len;

       len=str1.length()-1;

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

       {

           if(str1.charAt(i)!=str1.charAt(len-1))

           break;

       }

       if(i<=len)

       return true;

       else

       return false;

    }

    private String leftRev(String str1)

    {

       int i,len;

       String str2="";

       len=str1.length(); 

       for(i=len-1;i>=0;i--)

       str2=str2+str1.charAt(i);

       return str2;

    }

public static void main(String args[])

{

Ab obj=new Ab();

obj.show();

}

Monday, March 20, 2023

BlueJ Program On String Sorting And Swapping First And Last Character

 
                     A class SwapSort has been defined to string related manipulations on a input word.
                     Some of the members of the class are as follows
                     wrd: To store the word
                     len: length of the word
                     swapwrd: to store the swapped word
                     sortwrd: to store the sorted word
                     
                     SwapSort(): default constructor ti initialize the members with valid value
                     void readWord(): accept the word in upper case 
                     void sortWord(): sort the word in ascending order and stores in sortwrd
                     void swapChar(): Exchange the first and last character of the word and stores in swapwrd
                     void display (): Display all the words (original, swapped and sorted
                         

import java.util.*;
public class SwapSort
{
String wrd, swapwrd, sortwrd;
int len;
Scanner sc=new Scanner(System.in);
SwapSort()
{
    wrd="";
    sortwrd="";
    swapwrd="";
    len=0;
}

void readWord()
{
    System.out.print("\nEnter the word: ");
    wrd=sc.nextLine().toUpperCase();
}

void swapChar()
{
    len=wrd.length();
    System.out.println(wrd.charAt(len-1));
    swapwrd=swapwrd+wrd.charAt(len-1);
    for(int i=1;i<len-1;i++)
    swapwrd=swapwrd+wrd.charAt(i);
    swapwrd=swapwrd+wrd.charAt(0);
}
void sortWord()
{
    char c;
    char ch[]=new char[len];
    for(int i=0;i<len;i++)
    ch[i]=wrd.charAt(i);
    for(int i=0;i<len-1;i++)
    {
         for(int j=i+1;j<len;j++)
         {
             if(ch[i]>ch[j])
             {
                 c=ch[i];
                 ch[i]=ch[j];
                 ch[j]=c;
             }
         }
    }
    sortwrd=new String(ch);
                 
  }
       
void display()
{
    System.out.println("\nOriginal Word:"+wrd);
    System.out.println("\nSorted Word:"+sortwrd);
    System.out.println("\nSwapped Word:"+swapwrd);
}
    public static void main(String[] args) 
    {
    SwapSort ob=new SwapSort();
   ob.readWord();
   ob.swapChar();
   ob.sortWord();
   ob.display();
}
}

Monday, December 19, 2022

Display the words of a sentence in ascending order of their frequencies

import java.util.*;
class Main
{
    String str1,str[],str2[];
    int arr[];
    int i,j,len,c,k,x;
    String temp;
    StringTokenizer stk;
    Scanner sc=new Scanner(System.in);
    void show()
    {
        i=0;
        k=0;
        System.out.print("\nEnter Number of Sentences:");
        c=sc.nextInt();
        if(c<1||c>4)
        {
        System.out.print("\nInvalid Entry");
        System.exit(0);
    }
    Scanner sc=new Scanner(System.in);
    while(true)
    {
        System.out.print("Enter the sentences:");
        str1=sc.nextLine().trim();
        len=str1.length();
        if(str1.charAt(len-1)=='.'||str1.charAt(len-1)==','||str1.charAt(len-1)=='?'||str1.charAt(len-1)=='!')
        break;
        else
        System.out.print("\nTerminate the sentence with specified characters:");
    }
    stk=new StringTokenizer(str1,".,?! ");
    c=stk.countTokens();
    str=new String[c];
    str2=new String[c];
    arr=new int[c];
    for(i=0;i<c;i++)
    arr[i]=-1;
    i=0;
    while(stk.hasMoreTokens())
    {
       str[i++]=stk.nextToken();
    }
    for(i=0;i<c-1;i++)
        {
          for(j=i+1;j<c;j++)  
          if(str[i].compareTo(str[j])>0)
          {
              temp=str[i];
              str[i]=str[j];
              str[j]=temp;
            }
        }
    
    str1=str[0];
    k=1;
    for(i=1;i<c;i++)
    {
        if(str[i].equals(str1))
        k++;
        else
        {
            str2[x]=str1;
            arr[x++]=k;
            k=1;
            str1=str[i];
             }
    }
    str2[x]=str1;
    arr[x++]=k;
    for(i=0;i<x;i++)
  {
    for(j=i+1;j<x;j++) 
    {
        if(arr[i]>arr[j])
        {
    int t=arr[i];
  arr[i]=arr[j];
  arr[j]=t;
  temp=str2[i];
  str2[i]=str2[j];
  str2[j]=temp;
}
}
}
System.out.println("\nWord Frequency");
for(i=0;i<x;i++)
System.out.println(str2[i]+" "+arr[i]);
}
public static void main(String[] args)
{
    new Main().show();
}
}

Sample Input & Output

Enter Number of Sentences:3
Enter the sentences:this is a test. this is another, where are others?

Word Frequency
a 1
another 1
are 1
others 1
test 1
where 1
this 2
is 2

Sunday, December 18, 2022

Arrange the words of a sentence in descending order of their lengths

 import java.io.*;
import java.util.*;
public class Gcd 
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i,j,x;
    String str,sArr[]; 
    StringTokenizer stk;
    void accept() throws Exception
    {
        
        x=0;
        System.out.print("Enter a sentence: ");
         str=br.readLine(); 
         stk=new StringTokenizer(str);
         i=stk.countTokens();
         sArr=new String[i];
         while(stk.hasMoreTokens())
         {
             sArr[x++]=stk.nextToken();
            }
            for(i=0;i<x-1;i++)
            {
                 for(j=i+1;j<x;j++)
                 {
                     if(sArr[i].length()<sArr[j].length())
                     {
                         str=sArr[i];
                         sArr[i]=sArr[j];
                         sArr[j]=str;
                        }
                    }
                }
                System.out.println("\nWords in decending order according to length as follows");
                for(i=0;i<x;i++)
                System.out.println(sArr[i]);
            }
         
                  
public static void main(String args[])throws Exception
{
    Gcd ob=new Gcd();
    ob.accept();
}
}

Sample Input Output

Enter a sentence: this is another test of desending order

Words in desending order according to length as follows
desending
another
order
this
test
is
of


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);
   }
}

Friday, April 15, 2022

Write a program to bring all vowels at the beginning and then the other characters

 In this program, user will enter any sentence and all vowels will be placed at the beginning and then the other characters.

import java.io.*;
public class StringMan 
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i,len;
    String str,sVowel,sOther;
 
    void accept() throws Exception
    {
        sVowel="";
        sOther="";
        System.out.print("Enter Any Sentence: ");
         str=br.readLine(); 
         len=str.length();
         for(i=0;i<len;i++)
         {
             if(isVowel(str.charAt(i)))
             sVowel=sVowel+str.charAt(i);
             else
             sOther=sOther+str.charAt(i);
            }
            sVowel=sVowel+sOther;
            System.out.print("\nOriginal Sentence: "+str); 
            System.out.print("\nModified Sentence: "+sVowel); 
        }
           private boolean isVowel(char ch)
{
   boolean bool=false;
   ch=Character.toUpperCase(ch);
   switch(ch)
   {
       case 'A':
       case 'E':
       case 'I':
       case 'O':
       case 'U':
       bool=true;
    }
    return bool;
}            
public static void main(String args[])throws Exception
{
    StringMan ob=newStringMan();
    ob.accept();
}
}

Sunday, January 30, 2022

A Program to check if a string is unique or not

eg:Sample Input:COMPUTER
Sample Output:Unique string
import java.io.*;
class A
{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String str1;
     int i,j,len;
     void take() throws Exception
     {
          System.out.print("\nEnter the sentence: ");
          str1=br.readLine();
          len=str1.length();
          for(i=0;i<len-1;i++)
          {
               for(j=i+1;j<len;j++)
               {
                   if(str1.charAt(i)==str1.charAt(j))
                   break;
                }
                if(j<len)
                break;
            }
            if(i<len-1)
            System.out.print("\nNot Unique character string.");
            else
            System.out.print("\nUnique character string.");
        }
    }
  

How to remove common characters from a given string

  
Sample Input : Applications
Sample Output : Aplictons 
import java.io.*;
class A
{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String str1,str2="";
     int i,j,len;
     int arr[]=new int[26];
     void take() throws Exception
     {
          System.out.print("\nEnter the sentence: ");
          str1=br.readLine();
          len=str1.length();
          for(i=0;i<26;i++)
          arr[i]=0;
          for(i=0;i<len;i++)
          {
              char ch=str1.charAt(i);
              char ch1=Character.toLowerCase(ch);
              j=(int)ch1-97;
              if(arr[j]==0)
              {
              arr[j]=1;
              str2=str2+ch;
            }
        }
        System.out.print("\nModified string = "+str2);
    }
}

BlueJ Program On String - Uppercase Lowercase character

 Write a program to accept a sentence and check whether it is terminated by ‘.’ or ‘? ‘ or ‘!’ . Display the words using default delimiter (space) and the first character of each word should be converted into Upper Case.
Sample Input :
converting first character.
Sample Output:
converting Converting
first First
character Character
              
import java.io.*;
class A
{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String str1,str2;
     int i,len;
     char ch;
     void take() throws Exception
     {
         System.out.print("\nEnter sentence: ");
         str1=br.readLine();
         len=str1.length();
         if(str1.charAt(len-1)=='.' ||str1.charAt(len-1)=='?' ||str1.charAt(len-1)=='!' )
         str1=str1.substring(0,len-1);
         else
         {
              System.out.print("\nWrong Input");
              return;
            }
            str1=str1.toLowerCase();
            str1=str1+" ";
            while(true)
            {
                 int i=str1.indexOf(' ');
                 if(i<0)
                 break;
                 str2=str1.substring(0,i);
                 System.out.print("\n"+str2+ "  ");
                 ch=str2.charAt(0);
                 ch=Character.toUpperCase(ch);
                 str2=str2.substring(1);
                 System.out.print(ch+str2);
                 
                 str1=str1.substring(i+1);
                }
            }
        }
        

Saturday, November 6, 2021

BlueJ Program On String Manipulation

 This is a menu driven program with string manipulation.

import java.io.*;
 class String1
 {
     String s1,s2;  
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    private void takeName() throws IOException
    {
        System.out.print("\nEnter Name: ");
        s1=br.readLine();
    }
    private void wordCaseName()
    {
      char ch;
      s2="";
      int i,len;
      len=s1.length();
      for(i=0;i<len;i++)
      {
        ch=s1.charAt(i);
        if(i==0)
        s2=s2+Character.toUpperCase(ch);
        else if(ch==' ')
        {
            s2=s2+" ";
        s2=s2+Character.toUpperCase(s1.charAt(i+1));
        i++;
    }
    else
    s2=s2+Character.toLowerCase(ch);
}
s1=s2;
System.out.println("\nName in Sentence Case: "+s1);
}
    private void shortName()
    {
      char ch;
      s2="";
      int i,len,sp=0;
      len=s1.length();
      for(i=0;i<len;i++)
      {
          if(s1.charAt(i)==' ')
          sp++;
        }
           
      for(i=0;i<len;i++)
      {
        ch=s1.charAt(i);
        if(i==0)
        s2=s2+Character.toUpperCase(ch)+".";
        else if(ch==' ' && sp!=1)
        {
            s2=s2+" ";
        s2=s2+Character.toUpperCase(s1.charAt(i+1))+".";
        i++;
        sp--;
    }
    else if(ch==' ' && sp==1)
        {
            s2=s2+" ";
        s2=s2+Character.toUpperCase(s1.charAt(i+1));
        i++;
        sp--;
    }
    else if(sp==0)
    s2=s2+Character.toLowerCase(ch);
}
System.out.println("\nName in Short Form: "+s2);
}    
  private void replaceWord() throws IOException
  {
      String str1,str2,s3,s4;
      int i;
      s2="";
      s3=s1.trim()+" ";
      System.out.print("\nEnter the word which is to be replaced: ");
      str1=br.readLine();
      System.out.print("\nEnter the word which will replace: ");
      str2=br.readLine();
      while(true)
      {
           i=s3.indexOf(' ');
           if(i<0)
           break;
           s4=s3.substring(0,i);
           s3=s3.substring(i+1);
           if(s4.equalsIgnoreCase(str1))
           s2=s2+" "+str2;
           else
           s2=s2+" "+s4;
        }
        s2=s2.trim();
        System.out.println("\nModified Name: "+s2);
           
    }
    private void surnameFirst()
  {
      String s3,s4;
      int i;
      s2="";
      s3=s1;
      i=s3.lastIndexOf(' ');
      s2=s2+s3.substring(i+1);
      s3=s3.substring(0,i)+ " ";
      while(true)
      {
           i=s3.indexOf(' ');
           if(i<0)
           break;
           
           s2=s2+" "+s3.substring(0,i);;
           s3=s3.substring(i+1);
        }
        s2=s2.trim();
        System.out.println("\nModified Name With Surname First: "+s2);
    }
        public static void main(String args[])throws IOException
        {
            int choice=1;
            boolean bool=true;
            String parts1, parts2, parts3;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String1 ob=new String1();
            ob.takeName();
             while(choice!=0)
            {
                System.out.println("Enter 1 to view Name In Sentence Case\n 2 to view Name in Short Form\n 3 To replace a Word with Another\n 4 To Display Surname First and Then Name\n 0 For Exit: ");
                choice=Integer.parseInt(br.readLine());
                switch(choice)
                {
                     case 1:
                     ob.wordCaseName();
                     break;
                     case 2:
                     ob.shortName();
                     break;
                     case 3:
                     ob.replaceWord();
                     break;
                     case 4:
                     ob.surnameFirst();
                     break;
                     case 0:
                     System.out.print("\nEnd of Program.");
                     break;
                     default:
                     System.out.print("\nWrong Choice.");
                    }
                }
            }
        }
                

           Sample Input Output


Enter Name: amal KANTI maZUMder
Enter 1 to view Name In Sentence Case
 2 to view Name in Short Form
 3 To replace a Word with Another
 4 To Display Surname First and Then Name
 0 For Exit: 
1

Name in Sentence Case: Amal Kanti Mazumder
Enter 1 to view Name In Sentence Case
 2 to view Name in Short Form
 3 To replace a Word with Another
 4 To Display Surname First and Then Name
 0 For Exit: 
2

Name in Short Form: A. K. Mazumder
Enter 1 to view Name In Sentence Case
 2 to view Name in Short Form
 3 To replace a Word with Another
 4 To Display Surname First and Then Name
 0 For Exit: 
3

Enter the word which is to be replaced: Kanti
Enter the word which will replace: Kumar
Modified Name: Amal Kumar Mazumder
Enter 1 to view Name In Sentence Case
 2 to view Name in Short Form
 3 To replace a Word with Another
 4 To Display Surname First and Then Name
 0 For Exit: 
4

Modified Name With Surname First: Mazumder Amal Kanti
Enter 1 to view Name In Sentence Case
 2 to view Name in Short Form
 3 To replace a Word with Another
 4 To Display Surname First and Then Name
 0 For Exit: 
0

Saturday, July 3, 2021

Frequency Counting Of Words In A Sentence

 
Input a sentence from the user and count the number of times, the words “an” and “and” are present in the sentence. Design a class Frequency using the description given below: 

Class name: Frequency
Data members/variables:
text: stores the sentence
countand: to store the frequency of the word “and”
countan: to store the frequency of the word “an”
len: stores the length of the string

Member functions/methods:

Frequency(): constructor to initialize the instance variables

void accept(String n): to assign n to text, where the value of the parameter n should be in lower case.

void checkandfreq(): to count the frequency of “and”

void checkanfreq(): to count the frequency of “an”

void display(): to display the number of “and” and “an” with appropriate messages.

Specify the class Frequency giving details of the constructor(), void accepts(String), void checkandfreq(), void checkanfreq() and void display(). Also, define the main() function to create an object and call methods accordingly to enable the task.

import java.util.*;
class Frequency
{
    String text;
    int countan,countand,len;
    private frequency()
    {
        text="";
        countan=0;
        countand=0;
        len=0;
    }
    private void accept(String n)
    {
        text=n;
        StringTokenizer stk=new StringTokenizer(text," ");
        while(stk.hasMoreTokens())
        {
            String s=stk.nextToken();
            checkandfreq(s);
            checkanfreq(s);
        }
    }
    private void checkandfreq(String s)
    {
        if(s.equals("and"))
        countand++;
    }
    private void checkanfreq(String s)
    {
        if(s.equals("an"))
        countan++;
    }
    private void dsiplay()
    {
        System.out.println("The no. of an :"+countan);
        System.out.println("The no. of and :"+countand);
    }
    public static void main(String str[])
    {
        Scanner sc=new Scanner(System.in);
        Frequency ob=new Frequency();
        System.out.println("Enter the Sentence");
        String n=sc.nextLine();
        ob.accept(n);
        ob.dsiplay();
    }
}

Recursive Function And Case Change Of Letters In A Sentence

 
Design a class Change to perform string related operations. The details of the class are given below:

Class name: Change
Data Members/instance variables:
str: stores the word
newstr: stores the changed word
len: store the length of the word

Member functions:

Change(): default constructor

void inputword( ): to accept a word

char caseconvert (char ch): converts the case of the character and returns it

void recchange (int): extracts characters using recursive technique and changes its case using caseconvert () and forms a new word

void display (): displays both the words

(a) Specify the class Change, giving details of the Constructor ( ), member functions void inputword (), char caseconvert (char ch), void recchange (int) and void display (). Define the main () function to create an object and call the functions accordingly to enable the above change in the given word.

import java.io.*;
class change
{
    String str;
    static String newstr;
    int len;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    private change()
    {
        str="";
        newstr="";
        len=0;
    }
    private void inputword()throws IOException
    {
        System.out.println("Enter the word :");
        str=br.readLine();
        len=str.length();
        rechange(0);
    }
    private char caseconvert(char ch)
    {
        if(Character.isUpperCase(ch))
        ch=Character.toLowerCase(ch);
        else if(Character.isLowerCase(ch))
        ch=Character.toUpperCase(ch);
      return ch;  
    }
    private void rechange(int n)
    {
        char x=' ';
       if(n==len)
       return;   
        char ch=str.charAt(n);
        x=caseconvert(ch);
        newstr=newstr+x;
       n++;
       rechange(n);
    }
    public static void main(String args[])throws IOException
    {
        change ob=new change();
        ob.inputword();
        System.out.println(newstr);
    }
}

Words And Potential Strength of Each Words In Sentence

 

The potential of a word is found by adding the ASCII value of the alphabets.
(ASCII values of A to Z are 65 to 90).

Example: BALL
Potential = 66 + 65 + 76 + 76 = 283

Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ” only.
The words of sentence are separated by single blank space and are in UPPER CASE. Decode the
words according to their potential and arrange them in ascending order of their potential strength.
Test your program with the following data and some random data:

Example 1:
INPUT: HOW DO YOU DO?
OUTPUT: HOW = 238
DO = 147
YOU = 253
DO = 147
 DO DO HOW YOU

Example 2:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT: LOOK = 309
BEFORE = 435
YOU = 253
LEAP = 290
 YOU LEAP LOOK BEFORE

Example 3:
INPUT: HOW ARE YOU#
OUTPUT: INVALID INPUT

import java.util.StringTokenizer;
class Potential
{
    
    void main(String s)
    {
        int l=s.length();
        int n;
        int x=0;
        int c=0;
        String a[]=new String[100];
        int arr[]=new int[100];
        if(s.charAt(l-1)=='.'||s.charAt(l-1)==','||s.charAt(l-1)=='!'||s.charAt(l-1)=='?')
        s=s.substring(0,l-1);
        else
        {
           System.out.println("INVALID INPUT");
           System.exit(0);
        }
       
        StringTokenizer stk=new StringTokenizer(s);
        while(stk.hasMoreTokens())
        {
            
            String s1=stk.nextToken();
            int f=ascii(s1);
            a[c++]=s1;
            arr[x++]=f;
            
        }
        for(int i=0;i<c;i++)
        {
            System.out.println("Token: "+a[i]+ "  Potential: "+arr[i]);            
        }
        sort(a,arr,c);
    }
  private int ascii(String s1)
    {
        int z=0;
        int l=s1.length();
        for(int i=0;i<l;i++)
        {
            int c=s1.charAt(i);
            z=z+c;
        }
        return z;
    }
    private void sort(String a[],int arr[], int c)
    {
        
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<c-i-1;j++)
            {
                if(arr[j]>arr[j+1])
                {
                    int t=arr[j+1];
                    arr[j+1]=arr[j];
                    arr[j]=t;
                    
                    String s=a[j+1];
                    a[j+1]=a[j];
                    a[j]=s;
                }
            }
        }
             
        for(int i=0;i<c;i++)
        {
            System.out.print(a[i]+" ");            
        }
    }
public static void main(String args[])
{
 Potential obj = new Potential ();
obj.main("This is a test?");
}
}

Saturday, December 26, 2020

List Of BlueJ String And String Array Programs For ICSE And ISC Students

Programs On String Class And String Array Using Java Language For ICSE And ISC Students Are Posted Here. The Programs Can Be Executed On Bluej Or Any Other Editors. Here You Will Get Links To Number Of Programs Along With Explanation. Hope It Will Help Students.


To View The Programs, Click On The Links


Automorphic Number Checking Program Using String Object

Binary Addition

Bluej And C Language Program On Removing Blank Spaces From A Sentence

Caesar Cipher 

Changing Case Of Letters

Changing Character Case Of A Sentence After Every Vowel

Changing Upper Case Characters To Lower Case Using Recursive Function

Changing Vowels In A Sentence To Its Next Character

Changing Vowels With *

Character Which Occured Maximum Times In A String

Circular Decoding Of A Word Or Sentence

Competetion And Banner

Consecutive Letters From A Sentence

Converting Non Palindrome To Palindrome Words

Count Frequency Of Each Characters In A String 

Counting Frequency Of Words In A Sentence Without Using Stringtokenizer

Counting The Number Of Words From A Sentence Which May Contain Extra Spaces

Count Number Of Double Letter Words Is A String 

Counting Uppercase, Lowercase Characters, Digits And Special Characters In The Sentence

Generate Alternate Combination Of Uppercase Letters And Its Analysis

International Standard Book Number (ISBN) 

Longest And Shortest Words From A Sentence

Number Of Words Beginning And Ending With A Vowel In A Sentence

Palindrome And Special Word

Piglatin Word

Printing A Sentence Skipping The Vowels

Rearranging The Words Of A Text In Reverse Order

Rearranging The Word Bluej

Remove Extra Blank Spaces From A String

Remove First And Last Character Of Each Word From A Sentence

Remove Word From A Sentence

Removing All Punctuation Special Characters From A Sentence

Removing Vowels From A String

Replace A Word With Another Word From A Sentence

Replacing Only The Vowels Of A String With The Character Following It 

Reversing A Text – Words Of The Sentences Are Displayed In Reversed Order

 Reverse Each Word Of A Sentence

String Modification - Displaying An Entered Full Name In Short Form

String Palindrome Checking

String Which Is Terminated By Either “ . ” , “ ! ” Or “ ? ”

Toggle Cases Of Each Letter Of A Sentence Using Bluej

Validity Checking On Email Id

Word Counting Bluej Program Using Different Techniques

Word With Maximum Length In A Sentence

Words Starting And Ending Vowel

Words With Maximum And Minimum Vowels In A Sentence

Words With Vowel From A Sentence




String Array


Arranging Words Of A Sentence In Ascending Order Using Indexof And Substring Functions

Change All The Vowels In A Sentence With The Next Vowel

Merge Two Strings By Taking Alternate Words From Them

Sort The Words Of A Sentence In Ascending Order

Words From Extreme Sides Of The String And Generate A New String

Word Replacement In A Sentence



To Get More Programs:


ISC Practical Papers With Solution

ISC Theoretical Papers With Solution 

ICSE Theoretical Papers With Solution 

 

Friday, October 30, 2020

BlueJ Program On String And File Path With File Name

 Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input C: / Users / admin / Pictures / flower.jpg
Output path: C: / Users/admin/Pictures/
File name: flower
Extension: jpg 


       import java.util.*;
       class Ab
       {
            String str1,str2,str3;
            int i;
            Scanner sc=new Scanner(System.in);
            public void take()
            {
              System.out.print("\nEnter the file name with extension and complete path:");
              str1=sc.nextLine();
              i=str1.lastIndexOf('/');
              str2=str1.substring(0,i+1);
              System.out.print("\nPath of the file:"+str2);
              str2=str1.substring(i+1);// flower.jpg
              i=str2.indexOf('.');
              str3=str2.substring(0,i);
               str2=str2.substring(i+1);
              System.out.print("\n\nFile Name="+str3 + " Extension="+str2);
            }
        }
   Sample Input Output

Enter the file name with extension and complete path:C:/my folder/new folder/flower.jpg

Path of the file:C:/my folder/new folder/

File Name=flower Extension=jpg

BlueJ Programs On Function Overloading And String Functionms

 Design a class to overload a function Joystring( ) as follows :
(i) void Joystring (String s, char ch1 char ch2) with one string argument and two character arguments that replaces the character argument ch1 with the character argument ch2 in the given string s and prints the new string.
Example:
Input value of s = “TECHNALAGY”
ch1=‘A’,
ch2=‘O’
Output: TECHNOLOGY
(ii) void JJoystring(str1,ch1,ch2);
break;
}oystring (String s) with one string argument that prints the position of the first space and the last space of the given string s.
Example:
Input value of = “Cloud computing means Internet based computing”
Output: First index : 5
Last index : 36
(iii) void Joystring (String s1, String s2) with two string arguments that combines the two string with a space between them and prints the resultant string. Example :
Input value of s1 =“COMMON WEALTH”
Input value of s2 =“GAMES”
Output: COMMON WEALTH GAMES
(use library functions) 




 import java.util.*;
       class Ab
       {
            String str1,str2;
            char ch1,ch2;
            int choice;
            Scanner sc=new Scanner(System.in);
            public void take()
            {
                 
                   System.out.print("\nEnter your choice (1/2/3 for 1st function, 2nd function and 3rd function: ");
                   choice=sc.nextInt();
                   sc=new Scanner(System.in);
                   switch(choice)
                   {
                       case 1:
                       System.out.print("\nEnter the sentence:");
                       str1=sc.nextLine();
                       System.out.print("\nEnter the 1st Character:");
                       ch1=sc.next().charAt(0);
                       System.out.print("\nEnter the 2nd Character:");
                       ch2=sc.next().charAt(0);
                       Joystring(str1,ch1,ch2);
                       break;
                       
                       case 2:
                       System.out.print("\nEnter the sentence:");
                       str1=sc.nextLine();                       
                       Joystring(str1);
                       break;
                       
                       case 3:
                       System.out.print("\nEnter 1st sentence:");
                       str1=sc.nextLine();
                       System.out.print("\nEnter 2nd sentence:");
                       str2=sc.nextLine();
                       Joystring(str1,str2);
                       break;
                    }
                }
                private void Joystring(String s, char ch1, char ch2)
                {
                    s=s.replace(ch1,ch2);
                    System.out.print("\nModified sentence="+s);
                }
                private void Joystring(String s)
                {
                    int i=s.indexOf(' ');
                    System.out.print("\nFirst Space Location="+i);
                    i=s.lastIndexOf(' ');
                    System.out.print("\nLast Space Location="+i);
                }
                
                 private void Joystring(String s,String s1)
                {
                    s=s.concat(" ");
                    s=s.concat(s1);
                    
                    System.out.print("\nModified ="+s);
                }
            } 
            
            Sample Input Output


Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 1

Enter the sentence:this is a test

Enter the 1st Character:i

Enter the 2nd Character:x

Modified sentence=thxs xs a test
Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 2

Enter the sentence:this is a test

First Space Location=4
Last Space Location=9
Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 3

Enter 1st sentence:welcome

Enter 2nd sentence:home

Modified =welcome home

Subscribe via email

Enter your email address:

Delivered by FeedBurner