Saturday, November 26, 2011

IT13: exer2(my answer) JAVA language


Name: Janer, Jesson L.
Section: Set A
Time: TF(1:00-2:30)  
Exercise: 1

Code:

public class exercise2{
      public static void main (String[] args) {
           
            String msg = "Hello World, This is a test ! :)";
            string_classes.display_msg (5,msg);

System.out.println ("\nThe reverse string is: " +string_classes.reverse_msg (msg));
System.out.println ("\nThe no of vowels in a string is: "  +string_classes.no_of_vowels (msg));
System.out.println ("\nThe no of consonants in a string is:" +string_classes.no_of_consonants(msg));
System.out.println ("\nThe no of special characters in a string is: "  +string_classes.no_of_special_chars (msg));
System.out.println ("\nThe no of characters occurences:  ");
string_classes.char_occurences (msg);
      }
}

public class string_classes {
            public static void display_msg(int x, String msg){
                  for(int i = 0; i < x ; i++){
                        System.out.println ("\n" +msg);
                        }
                  }

            public static String reverse_msg(String msg){
                  String str_reverse = " ";
                  //Enter your solution here.....
                 str_reverse = new StringBuffer(msg).reverse().toString();
                 return str_reverse;
                  }

            public static int no_of_vowels(String msg){
                  int nvowels = 0;
                  //Enter your solution here.....
                  String tester="aeiou";
             for(int a=0;a<msg.length();a++){
                   for(int b=0;b<tester.length();b++){
                     if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))nvowels++;
                              }
                }
                     return nvowels;
                  }


            public static int no_of_consonants(String msg){
                  int nconsonants = 0;
                  //Enter your solution here.....
              String tester="bcdfghjklmnpqrstvwxyz";
              for(int a=0;a<msg.length();a++){
                  for(int b=0;b<tester.length();b++){
                   if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))nconsonants++;
                    }
                }
                        return nconsonants;
                  }

            public static int no_of_special_chars(String msg){

                  int sp_chars = 0;
                  //Enter your solution here.....
                  String tester="~`!@#$%^&*()-_+={}[];:',.></?|";
                for(int a=0;a<msg.length();a++){
                   for(int b=0;b<tester.length();b++){
                     if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))sp_chars++;
                              }
                   }
                        return sp_chars;
                  }


            public static void char_occurences(String msg){
            //Enter your code here...
            /* Sample output: Hello World, This is a test ! :
             *H - 2, e - 2, l - 3, o - 2, W - 1, r - 1 d - 1,
             *T - 3, i - 2, s - 3, a - 1
             *
             */
    String tester="abcdefghijklmnopqrstuvwxyz";
    boolean t=true,th=true;
    int no=0;
    String char_passed=" ";
          
    System.out.println(msg);
          
    for(int a=0;a<msg.length();a++){
          
            for(int b=0;b<tester.length();b++){
           if(msg.toLowerCase().charAt(a)==tester.charAt(b)){
                 t=true;
                 b=27;
                }else{
                 t=false;
                }
           }
          
            if(t==true){
        for(int c=0;c<msg.length();c++){
           if(msg.toLowerCase().charAt(a)==msg.toLowerCase().charAt(c))no++;
             }
                    
        for(int d=0;d<char_passed.length();d++){
           if(msg.toLowerCase().charAt(a)==char_passed.charAt(d)){
                th=false;
                d=100;
                }
           }
                  
           if(th==true){
               System.out.println(msg.charAt(a)+ "-"+no);
               char_passed=char_passed+msg.toLowerCase().charAt(a);
                   }
          }
                    t=true;
                    th=true;
                    no=0;

     }

       }

}

1 comment:

  1. this code is intended upon knowing how many vowels,consonants and special characters are there in a string..at the end part the code will guide upon knowing how many times a certain character occur in the string

    ReplyDelete