Count Occurrences of Characters in String - JAVA

Count Occurrences of Characters in String - JAVA


Make a Simple program which count the occurrences of Each Character in String . It will take one STRING as an INPUT.

Example:
INPUT    :   kapilshukla
OUTPUT : 
k – 2
a – 2
p – 1 ….

class prog2
{
public static void main(String args[])
{

char c[]=new char[10];

for(int i=0;i<args[0].length();i++)
{
        //This code will work if character is not @

if(args[0].charAt(i)!='@')
{

          //Count variable will count occurrences of Character 
int count=1;

for(int j=i+1;j<args[0].length();j++)
{
//This code will compare two character
if(args[0].charAt(i)==args[0].charAt(j))
{
count++;
}
}
       System.out.println(args[0].charAt(i)+ " - " + count);
}
      //This Code will replace all counted character with @
args[0] = args[0].replace(args[0].charAt(i), '@');
}
}

}

Calculator Application


Make a Simple Calculator using Command Line Argument in Java.

It will take input from Command Line in series of 
OPERAND – OPERATOR – OPERAND.

Example:

    INPUT : 15 + 29 – 30


    OUTPUT : 14
class first
{
public static void main(String args[])
{
int i=0;
int no;
no =Integer.parseInt(args[i++]);

                while(i<args.length)
{
if(args[i].equals("+"))
{
no = no + Integer.parseInt(args[i+1]);
}
else if(args[i].equals("-"))
{
no = no - Integer.parseInt(args[i+1]);
}
else if(args[i].equals("x"))
{
no = no * Integer.parseInt(args[i+1]);
}
else
{
no = no / Integer.parseInt(args[i+1]);
}
i = i + 2; 
}
System.out.println("\n Calculation : " + no);
}
}

OUTPUT :

E:\>javac first.java

E:\>java first 10 + 20 - 25


 Calculation : 5

Logic Building with JAVA

Date : 27 / 8 /2014


Logic Building with JAVA

Activity – I


Prepared by

Prof. Kapil Shukla




AIM of Activity
  • Team Building
  • Concreting Logic
  • Clear Presentation of Idea
  • Converting Logic into CODE
  • Better Understanding of Problem
  • Thinking about Alternative Solution

[1]
Calculator Application

Make a Simple Calculator using Command Line Argument in Java.

It will take input from Command Line in series of OPERANDOPERATOROPERAND.

Example:
   INPUT : 15 + 29 – 30
   OUTPUT : 14

[2]
Occurrences of Characters

Make a Simple program which count the occurrences of Each Character in String .

It will take one STRING as an INPUT.

Example:
   INPUT : kapilshukla
   OUTPUT :        k – 2
     a – 2
     p – 1 ….
[3]

Concatenate Strings and SORT 

Make a Simple program which concatenate two String and then Perform SORT operation on Final String.

It will take TWO STRINGS as an INPUT.

Example:
   INPUT : gardi mca
   OUTPUT : aacdgimr (IF possible – acdgimr)

Convert Numbers in to Words

Definition : Convert Given Number in to Words.

1347 - ONE THREE FOUR SEVEN

Solution : 

class Cal
{
public static void main( String args[])
{
        int i=0,n=0;
n=Integer.parseInt(args[0]);
        
        String str[] = {"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};

int []arr;
        arr=new int[10];

        System.out.print(n + " : ");

for(i=0;n>0;i++)
 {
 arr[i]=n%10;
 n = n/10;
 }
        
        for(i=i-1; i >=0; i --)
          {
           System.out.print(str[arr[i]]+ " ");
 }
}
}

OUTPUT :


Compile : > javac cal.java

Run       : >java Cal 1387

1387 : ONE THREE EIGHT SEVEN