Thursday, 19 June 2014

java program to remove duplicates from a character array .......

here is the source code ....

       

class removeDuplicates {
 public static void main(String args[]) {
  char[] chr = {
   'g',
   'a',
   'a',
   'y',
   'a',
   'd',
   'a',
   'v'
  };
  boolean[] ch = new boolean[256];
  String str1 = new String(chr);
  System.out.println("your entered string " + str1);
  int len = chr.length;
  for (int s = 0; s < len; s++) {
   int g = chr[s];
   if (ch[g]) {
    chr[s] = '\0';
    for (int a = s; a < len; a++) {
     if (a == len - 1) {
      break;
     }
     chr[a] = chr[a + 1];
     chr[a + 1] = '\0';
    }
    len--;
    s--;
   } else {
    ch[g] = true;
   }
  }
  String str = new String(chr);
  System.out.print(str);
  //System.out.println(str.length());
 }
}
       
 

Saturday, 14 June 2014

java program to print a given pattern of " * " and " _ " as below .......

to print following pattern...
* * * * * *
* * * *_ _
* *_ _ _ _
_ _ _ _ _ _      

here is the code ..

       
import java.io.*;
class asteriskprint {
 public static void main(String args[]) {
  for (int i = 0; i < 4; i++) {
   for (int k = 0; k < 6 - 2 * i; k++) {
    System.out.print("*");
   }
   for (int h = 0; h < 2 * i; h++)
    System.out.print("_");
   System.out.println("\n");
  }
 }
}