Thursday, 7 January 2016

How to calculates the total count of all characters within input string through java ??

package org.cmania;

public class CharacterCount
{
public static void main(String[] args)
{
        String inputString="my name is nishant";

       System.out.println("inputString is : "+inputString);
       System.out.print("Output is : ");
     
                characterCount(inputString);
 }

    /**
    * Method calculates count of all characters in inputString.
    */

   public static void characterCount(String inputString)
   {
   
      char[] inputAr=inputString.toCharArray();
      int count=0,arLength;
   
       arLength=inputAr.length;
       for(int x=0; x<arLength; x++)
       {
         char ch=inputAr[x];
         for(int y=x+1; y<arLength; y++)
         {
            if(inputAr[y]==ch)
            {
            //if we have find same character again in string
                for(int z=y; z< arLength-1; z++)   //shift characters left.
                    inputAr[z]=inputAr[z+1];
                arLength--;   //as characters have been reduce arLength.
                y=x;   //done to tackle case if occurrence of character is more than once in string.
            }
         }
       }
     
     
       /*
       *//**
        * Method calculates count of all characters in inputString.
        *//*
     
       public static void characterCount(String inputString){
          Map<Character, Integer> map=new LinkedHashMap<Character, Integer>(); //LinkedHashMap used so that we could maintain insertion order.
          char[] inputAr=inputString.toCharArray();
       
          for(int i=0;i<inputAr.length;i++){
              char ch=inputAr[i];
              if(map.containsKey(ch))
                     map.put(ch, map.get(ch) +1);
              else
                     map.put(ch, 1);
                   
          }
         
         * Till this point of program, we have stored all unique characters in map as key & corresponding value representing count of character.
           
         
          Iterator<Character> charIterator=map.keySet().iterator();
          while(charIterator.hasNext()){
              char ch=charIterator.next();
              System.out.print(ch+"="+map.get(ch)+" ");
          }
       */
     
       /*
          * Till this point of program, inputAr's first arLength number of elements are unique.
       */
     
       for(int x=0;x<arLength;x++)
       {
          count=0;
          for(int y=0; y<inputAr.length; y++)
          {
            if(inputAr[x]==inputString.charAt(y))
              count++;
          }
         System.out.print(inputAr[x]+"="+count+" ");
       }
     
     
   }
 
}

3 comments:

  1. Can you provide me the code for below Programming Test Details ???

    Design: A Valet parking system is a system where:
    A customer is given a ticket on parking, and they can redeem the ticket to get their vehicles back.
    Parking lots have spaces in 5 sizes. XS,S,M,L,XL.
    There are 7 types of vehicles that come in: XXS, XS,M,L,XL,XL, XXL
    XS & XXS vehicles can be parked in XS spot.
    S, XS & XXS vehicles can be parked in S spot.
    M, S, XS & XXS vehicles can be parked in M spot.
    L, M, S, XS & XXS vehicles can be parked in L spot.
    XL, L, M, S,XS & XXS vehicles can be parked in XL spot.

    Deliverable:
    UML: (Reference: http://creately.com/blog/diagrams/uml-diagram-types-examples/ )
    Use Case Diagram
    Class Diagram
    Sequence Diagram
    Activity Diagram (Optional)
    State Diagram (Optional)
    Implementation
    Full API implementation of the design in C# or Java.
    Implementation of the API client for each use case.
    Please provide Images & source of the design along source code of the implementation.

    All the UML designs can be made using https://www.draw.io/. You may chose any other tool of your preference.

    ReplyDelete
  2. Different way to find the First non repeated character in the String ?

    Way 1 :

    public static char getFirstNonRepeatedChar(String str)
    {
    Map counts = new LinkedHashMap<>(str.length());
    for (char c : str.toCharArray()) {
    counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1);
    }
    for (Entry entry : counts.entrySet()) {
    if (entry.getValue() == 1) {
    return entry.getKey();
    }
    }
    throw new RuntimeException("didn't find any non repeated Character");
    }

    i.e.

    /* * Using LinkedHashMap to find first non repeated character of String * Algorithm : * Step 1: get character array and loop through it to build a * hash table with char and their count. * Step 2: loop through LinkedHashMap to find an entry with * value 1, that's your first non-repeated character, * as LinkedHashMap maintains insertion order. */


    Way 2 :

    public static char firstNonRepeatingChar(String word) {
    Set repeating = new HashSet<>();
    List nonRepeating = new ArrayList<>();
    for (int i = 0; i < word.length(); i++) {
    char letter = word.charAt(i);
    if (repeating.contains(letter)) {
    continue;
    }
    if (nonRepeating.contains(letter)) {
    nonRepeating.remove((Character) letter);
    repeating.add(letter);
    }
    else {
    nonRepeating.add(letter);
    }
    }
    return nonRepeating.get(0);
    }

    /* * Using HashMap to find first non-repeated character from String in Java. * Algorithm : * Step 1 : Scan String and store count of each character in HashMap * Step 2 : traverse String and get count for each character from Map. * Since we are going through String from first to last character, * when count for any character is 1, we break, it's the first * non repeated character. Here order is achieved by going * through String again. */


    Way 3 :

    public static char firstNonRepeatedCharacter(String word)
    {
    HashMap scoreboard = new HashMap<>();
    // build table [char -> count]
    for (int i = 0; i < word.length(); i++) {
    char c = word.charAt(i);
    if (scoreboard.containsKey(c)) {
    scoreboard.put(c, scoreboard.get(c) + 1);
    } else {
    scoreboard.put(c, 1);
    }
    }
    // since HashMap doesn't maintain order, going through string again

    for (int i = 0; i < word.length(); i++) {
    char c = word.charAt(i);
    if (scoreboard.get(c) == 1) {
    return c;
    }
    }
    throw new RuntimeException("Undefined behaviour");
    }
    }

    ReplyDelete
    Replies
    1. /* * Using HashMap to find first non-repeated character from String in Java. * Algorithm : * Step 1 : Scan String and store count of each character in HashMap * Step 2 : traverse String and get count for each character from Map. * Since we are going through String from first to last character, * when count for any character is 1, we break, it's the first * non repeated character. Here order is achieved by going * through String again. */

      Delete

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...