www.fatihkabakci.com

Personal Website and Computer Science TUR EN

CS480L MID Q8 String Frequency

Last update: 11/9/2016 10:58:00 AM

By Fatih KABAKCI

The following Java program has been published to figure out how many times each of letters appears in a given string by calculating frequency of them. The program basically uses a TreeMap which is one of the JCF (Java Collection Framework) objects to store characters to avoid counting them more than once. The TreeMap object alphaMap is used to store characters and their frequency values as key value pairs. First, the program checks if each character that is analyzed in string is counted before by looking up in alphaMap. If it finds out that related character have already used before, then it skips and passes on another character to count it. Otherwise, it counts a new character, then it saves it in alphaMap. Finally, helping a Iterator object, all data is printed in console. The method count() returns the actual frequency value of a character in the string.

package com.fatihkabakci.week05;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * 
 * @author fkabakci
 *
 */
public class StringFrequency {
	private String text;

	public StringFrequency(String text) {
		this.text = text;
	}

	public int count(String appear) {
		int counter = 0;
		int length = text.length();
		int alength = appear.length();

		for (int i = 0; i < length + 1 - alength; i++) {
			if (text.substring(i, i + alength).equals(appear))
				counter++;
		}
		return counter;
	}
	
	public void listFrequencyForLetters() {
		TreeMap<Character, Integer> alphaMap = new TreeMap<Character, Integer>();
		for (char ch : text.toCharArray()) {
			if (!alphaMap.containsKey(ch)) {
				if (Character.isLetter(ch)) {
					int n = count(String.valueOf(ch));
					alphaMap.put(ch, n);
				}
			}
		}

		Set<Entry<Character, Integer>> alphaSet = alphaMap.entrySet();
		Iterator<Entry<Character, Integer>> it = alphaSet.iterator();
		while (it.hasNext()) {
			Entry<Character, Integer> node = it.next();
			char c = node.getKey();
			int n = node.getValue();
			System.out.println("The character " + c + " has occured for " + n + " times");
		}
	}

	public static void main(String[] args) {
		final String text = "Java is a object oriented programming language. We can create objects in Java !!";
		StringFrequency str = new StringFrequency(text);
		str.listFrequencyForLetters();
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.