Interviewinterview program javaTop 9 counting program in Java

Top 9 counting program in Java

This is set of program in java, You can also refer our previous post

Write a program in Java to count no. of vowels and consonants.

package com.onurdesk.count.programs;

import java.util.Scanner;

public class CountNoOfVowelsAndConsonentsInString {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
                public static void main(String[] args) {
		int vCount = 0, cCount = 0;
		Scanner scan = new Scanner(System.in);
		System.out.println("enter any Sentence");
		String str = scan.next();
		str.toLowerCase();
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
					|| str.charAt(i) == 'u') {
				vCount++;
			} else if (str.charAt(i) > 'a' && str.charAt(i) < 'z') {
				cCount++;
			}
		}

		System.out.println("vowels are " + vCount);
		System.out.println("Consonents are " + cCount);
               System.out.println("Thanks for visiting https://onurdesk.com");
	}

}
program in Java

Write a program in Java to count no. of vowels in string.

package com.onurdesk.count.programs;

import java.util.Scanner;

public class CountNumberOfVowles {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter any sentance");
		String s = sc.nextLine();
		System.out.println("Number of Vowels in sentance is " + vowelCount(s));
	}

	private static int vowelCount(String s) {
		char[] str = s.toCharArray();
		int size = str.length;
		int v_count = 0;
		for (int i = 0; i < size; i++) {
			if (str[i] == 'A' || str[i] == 'I' || str[i] == 'O' || str[i] == 'E' || str[i] == 'U' || str[i] == 'a'
					|| str[i] == 'i' || str[i] == 'o' || str[i] == 'e' || str[i] == 'u') {
				v_count++;
			}
		}
		return v_count;
               System.out.println("Thanks for visiting https://onurdesk.com");

	}

}
program in Java

Write a program in Java to count no. of words in a string.

package com.onurdesk.count.programs;

import java.util.StringTokenizer;

public class CountNumberOfWordsInString {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {
		String x = "This article is for counting programs ";
                StringTokenizer st = new StringTokenizer(x);
		System.out.println(st.countTokens());
		while (st.hasMoreTokens()) {
			System.out.print(st.nextToken());
		}

		String y[] = x.split("");
		System.out.println(y);
                System.out.println("Thanks for visiting https://onurdesk.com");
	}

}
program in Java

Write a program in Java to count the occurrence each character.

package com.onurdesk.count.programs;

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

public class CountOfOccuranceEachChar {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {

		String str = "Learn Java Coding";
		countOfOccuranc1e(str);
	}

	private static void countOfOccuranc1e(String str) {
		char[] ch = str.toCharArray();
		Map<Character, Integer> map = new LinkedHashMap<>();
		for (Character c : ch) {
			if (map.containsKey(c)) {
				map.put(c, map.get(c) + 1);
			} else {
				map.put(c, 1);
			}
		}
		Set<Entry<Character, Integer>> s = map.entrySet();
		Iterator<Entry<Character, Integer>> itr = s.iterator();
		while (itr.hasNext()) {
			Map.Entry<Character, Integer> entry = (Map.Entry<Character, Integer>) itr.next();
			System.out.println(entry.getKey() + "------" + entry.getValue());
		}
                System.out.println("Thanks for visiting https://onurdesk.com");

	}

}
program in Java

Write a program in Java to count the occurrence of each element.

package com.onurdesk.count.programs;

public class CountOfOccuranceEachElement {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int[] array = { 1, 6, 2, 2, 1, 7, 5, 3, 4, 5, 6, 6, 2 };
		countOfElements(array);
	}

	public static void countOfElements(int[] a) {
		int fr[] = new int[a.length];
		int visited = -1;
		int count;
		for (int i = 0; i < a.length; i++) {
			count = 1;
			for (int j = i + 1; j < a.length; j++) {
				if (a[i] == a[j]) {
					count++;
					fr[j] = visited;
				}
			}
			if (fr[i] != visited) {
				fr[i] = count;
			}
		}
		System.out.println("Number of Occurance ");
		for (int i = 0; i < fr.length; i++) {
			if (fr[i] != visited) {
				System.out.println("  " + a[i] + "     |     " + fr[i]);
			}
		}
               System.out.println("Thanks for visiting https://onurdesk.com");
	}
}
program in Java

Write a program in Java to count non-duplicate words.

package com.onurdesk.count.programs;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class CountOfOccuranceWords {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {
		String str = "Learn Java Coding And Java Concepts Learn Programmming Coding";
		countWords(str);
	}

	private static void countWords(String str) {
		String[] words = str.split(" ");
		Map<String, Integer> map = new LinkedHashMap<>();
		for (String word : words) {
			if (map.containsKey(word)) {
				map.put(word, map.get(word) + 1);
			} else {
				map.put(word, 1);
			}
		}

		System.out.println("Non Duplicate Words are ");
		Set<Entry<String, Integer>> set = map.entrySet();
		Iterator<Entry<String, Integer>> itr = set.iterator();
		while (itr.hasNext()) {
			Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) itr.next();
			if (entry.getValue() == 1) {
				System.out.println(entry.getKey());
			}

		}
               System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program in Java to count vowels, consonants, and special characters.

package com.onurdesk.count.programs;

import java.util.Scanner;

public class CountVowConstSymbolsInString {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */


	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter any String");
		String x = input.nextLine();
		x = x.toUpperCase();
		char[] y = x.toCharArray();
		int size = y.length;
		int vowcont = 0;
		int constcont = 0;
		int splcont = 0;
		int i = 0;
		while (i != size) {
			if (y[i] >= 'A' && y[i] <= 'Z') {
				if (y[i] == 'A' || y[i] == 'I' || y[i] == 'O' || y[i] == 'E' || y[i] == 'U') {
					++vowcont;
				} else {
					++constcont;
				}

			} else {
				++splcont;
			}
			++i;

		}
		System.out.println(x);
		System.out.println("Vowles are :" + vowcont);
		System.out.println("Constants are :" + constcont);
		System.out.println("Special characters are :" + splcont);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}

Write a program in Java to count words without using methods.

package com.onurdesk.count.programs;

import java.util.Scanner;
public class CountWordsWithoutUsingMethods {
/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static int countWords(String str) {
		char SPACE = ' ';
		int countWords = 1;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == SPACE) {
				countWords++;
			}
		}

		return countWords;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter sentance");
		String str = sc.nextLine();
		System.out.println(countWords(str));
                System.out.println("Thanks for visiting https://onurdesk.com");
	}

}

Write a program in Java to count words without using methods. (Type2)

package com.onurdesk.count.programs;

import java.util.Scanner;

public class CountWordsWithoutUsingMethods1 {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static int countWords(String str) {
		String SPACE = " ";
		int countWords = 0;
		String words[] = str.split(SPACE);

		for (String word : words) {
			if (word.trim().length() > 0)
				;
			{
				countWords++;
			}
		}

		return countWords;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter sentance");
		String str = sc.nextLine();
		System.out.println(countWords(str));
        System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article