Skip to main content

Posts

FINDING MISSING ELEMENTS IN A STRING

import java.io.*; import java.lang.*; class Misord { public static void main(String args[])throws IOException { check chk= new check(); String str1=chk.input(); String str4=chk.ord(str1); chk.mis(str4); } } class check { public String input()throws IOException { String x = " "; System.out.println("Enter the String"); DataInputStream in = new DataInputStream(System.in); x= in.readLine(); return(x); } public String ord(String str1) { char ch=' '; String str2=str1; String str3=" "; int l=str2.length(); int i,j; for(i=65;i<=90;i++) { for(j=0;j<=l-1;j++) { ch = str2.charAt(j); { if(((int)ch==i)||((int)ch==i+32))//change here has been done { str3=str3+ch; str3=str3.trim(); } } } } System.out.println(str3); return(str3); } public void mis(String str4) { String str5=str4.trim(); String s1=" "; int i=0,j=0,f=0,p=0; int l=str5.length(); System.out.println(l); for(i=65;i<90;i++) { p=0;...

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 ********* ****  **** ***      *** **         ** *             * **         ** ***      *** ****  **** **...