Wednesday, 15 July 2015

Java Programs Made Easy

Q)... Find no of characters in a given string which are not repeated.
input: "hello"
output: 3



  1. package Set1;
  2. public class ClassSet26 {
  3. public static int noOfnonRepeatedCharacters(String s1){
  4.   StringBuffer sb=new StringBuffer(s1);
  5.   for(int i=0;i<sb.length();i++){
  6.    int n=0;
  7.    for(int j=i+1;j<sb.length();j++)
  8.     if(sb.charAt(i)==sb.charAt(j)){
  9.      sb.deleteCharAt(j);
  10.      n++; }
  11.     if(n>0){
  12.      sb.deleteCharAt(i);
  13.      i--;}  }
  14.   return sb.length();
  15.  }
  16. public static void main(String[] args) {
  17.   String s1="preethi";
  18.   System.out.println(noOfnonRepeatedCharacters(s1));
  19.  }
  20. }