Skip to main content

Posts

Program in java to extract words and print the words starting with a inputted character

import java.io.*; import java.lang.*; class Ser { public static void main(String args[])throws Exception { DataInputStream dd= new DataInputStream(System.in); System.out.println("enter a string "); String str=dd.readLine(); System.out.println("enter a character"); char ch=(char)System.in.read(); s o =new s(); if(o.extract(str,ch)==true) o.display(); else System.out.println("NOT FOUND"); } } class s { String s2=""; public boolean extract(String str,char ch2) { str=str+" "; String s3=""; boolean b=false; int i; for(i=0;i<str.length();i++) { char ch= str.charAt(i); if(ch != ' ') s3=s3+ch; else { if(s3.charAt(0)==ch2)    { b=true; s2=s2+" "+s3;     } s3="";  } } return b; } public void display() { System.out.println(s2); } }

StringBuffer in java

import java.io.*; class a { public static void main(String[] args) { StringBuffer s=new StringBuffer(); System.out.println(s.capacity()); System.out.println(s.length()); StringBuffer s1=new StringBuffer(100); System.out.println(s1.capacity()); StringBuffer s2=new StringBuffer("npgc"); System.out.println(s2); System.out.println(s2.length()); System.out.println(s2.capacity()); } } OUTPUT 16 0 100 npgc 4 20

Printing Consecutive alphabets in a String

import java.io.*; import java.lang.*; class s { public void asc(String t) {  t=t.toUpperCase(); int p,f=0,q,d; for(int i=65;i<=90;i++) { f=0; for(int j=0;j<t.length()-1;j++) { p=(int)t.charAt(j); q=(int)t.charAt(j+1); d=q; if(d==65) d=91; if((p==i)&&(d==i+1)) { f+=1; System.out.println((char)p+","+(char)q); } } } } } class Cons { public static void main(String args[])throws Exception { s ob=new s(); DataInputStream d = new DataInputStream(System.in); String t; System.out.println("Enter a String"); t= d.readLine(); ob.asc(t); } }

Frequency of alphabets in a string

import java.io.*; import java.lang.*; class s { public void asc(String t) { int p,f=0; for(int i=65;i<=90;i++) { f=0; for(int j=0;j<t.length();j++) { p=(int)t.charAt(j); if((p==i)||(p==i+32)) f+=1; } if(f!=0) System.out.println("FREQUENCY OF "+(char)i+" is "+f+"\t times" ); } } } class Fre { public static void main(String args[])throws Exception { s ob=new s(); DataInputStream d = new DataInputStream(System.in); String t; System.out.println("Enter a String"); t= d.readLine(); ob.asc(t); } }

Java program of common difference

import java.io.*; class commondifference { public static void main(String args[]) throws Exception { int f=1,d=0,d2=0,n,r,r2; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER a number"); n=Integer.parseInt(br.readLine()); do { r=n%10; r2=(n/10)%10; d=r-r2; if(d2==0||d2==d) d2=d; else if (d2!=d)  { f=2; break;   } n=n/10; }while(n>9); if(f==2) System.out.println("No Common Difference"); else System.out.println("Common difference is" + d2); } }

Java program for printing a pattern of java

import java.io.*; class pattern { public static void main(String args[]) throws Exception { int n,i,j,s,t=-1; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER a number"); n=Integer.parseInt(br.readLine()); for(i=n;i>=1;i--) { for(j=i;j>=1;j--) System.out.print("*"); for(s=1;s<=t;s++) System.out.print(" "); t+=2; j=i; if(j==n) j=j-1; for(;j>=1;j--) System.out.print("*"); System.out.println(); } t=t-2; for(i=2;i<=n;i++) { for(j=1;j<=i;j++) System.out.print("*"); t-=2; for(s=t;s>=1;s--) System.out.print(" "); j=i; if(j==n) j=j-1; for(;j>=1;j--) System.out.print("*"); System.out.println(); } } } ENTER a number ********* ****  **** ***      *** **         ** *             * **         ** ***      *** ****  **** **...

Snakes and ladder game in java

import java.io.*; class game { int pos,p,f; String pname; game(String name) { pname=name; pos=0; f=0; } public void play(int val)  { if(val==0) val=1; pos=pos+val; System.out.println(pname+"\t is at \t"+ pos + " position"); f=0; switch (pos)   { case 4:  pos+=10;  f=4; break; case 9:  pos+=22; f=22; break; case 17: pos-=17; f=-17; break; case 19: pos+=19; f=19; break; case 21: pos+=21; f=21; break; case 28: pos+=56; f=56; break; case 51: pos+=16; f=16; break; case 54: pos-=20; f=-20; break; case 62: pos-=43; f=-43; break; case 64: pos-=4;  f=-4; break; case 71: pos+=20; f=20; break; case 80: pos+=20; f=20; break; case 87: pos-=63; f=-63; break; case 93: pos-=20; f=-20; break; case 95: pos-=20; f=-20; break; case 98: pos-=19; f=-19; break; } if(f>0) System.out.println(pname+"\t has climbed a ladder and reached to\t"+ pos + " with "+f+ " points increment"); else if (f<0) System.out.println...