Count letter in java

Java Program to Count Letters, Digits, Spaces And Other Characters From A String

Write a Java program to count the letters, spaces, numbers and other character of an input String.

Steps to solve this program:
  1. Take inputs.
  2. Apply for loop for each and every character checking process inside method.
  3. Count letter inside the string.
  4. Count numbers inside the string
  5. Count spaces and other characters inside the string.
  6. Call method inside main method
  7. Display all the counting variables

Take all string inputs and initialize all the counting variable as 0.

Using for loop from 0 to string length for iteration process inside count method.

Using if and isLetter() to count the total letters present inside string.

Using if and isDigit() to count the total digits present inside String.

Using if and isSpaceChar() and else part is used to count the total spaces and other characters present inside string.

Inside main method call count(test) for displaying every count variables.

After storing all values inside counting variable now print all the values inside method.

System.out.println(«letter: » + letter); System.out.println(«space: » + space); System.out.println(«number: » + num); System.out.println(«other: » + otherchat);

Full Java Code:

import java.util.Scanner; public class Javaexcercise < public static void main(String[] args) < String test = "@Atechdaily.com 123456"; count(test); >public static void count(String x) < char[] ch = x.toCharArray(); int letter = 0; int space = 0; int num = 0; int otherchat = 0; for (int i = 0; i

Источник

How to Count Characters in a String in Java?

While programming in Java, there exist chances that you need to check the total number of characters of the specified string. In such a scenario, you must count the string characters to determine their length. This functionality can also be utilized in several Java applications where it is required to delete unwanted or duplicated characters of a string.

This tutorial will describe the methods to count the characters in strings in Java.

How to Count Characters in a String in Java?

To count the string’s characters, there are some methods listed as follows:

We will now check out each of the above-mentioned methods one by one.

Method 1: Count Characters in a String in Java Using for Loop

Using the “for” loop for counting the characters of a string is the simplest method utilized by programmers. This method will iterate according to the string’s length and count its characters.

Example
In this example, we will count the characters of the string with white spaces. For this purpose, we will create a String type variable named “string” and an integer type variable named “chCount” initialized with value 0:

Читайте также:  What is data model in java

String string = «Welcome To Linux Hint” ;
System.out.println(» String : » + string);

Then, we will iterate the string until the length of the string using for loop and count the characters “chCount” increment value:

Lastly, we will print the value of the “chCount” variable:

In the defined string, there are 18 characters and three spaces. Therefore, the output displayed “21” as the total number of string characters, including spaces:

Want to try out Java methods for counting characters? Have a look at the below-given sections.

Method 2: Count Characters in a String in Java Using String.length() Method

Another method to count a character in a string is the “length()”. This method belongs to the String class; that’s why it is called using the String class object.

Example
In this example, we will consider two cases:

  • Counting string characters including white spaces
  • Counting string characters without spaces

For the first case, we will create an integer type variable named “newString” that stores the length of the full string by calling the “string.length()” method. This method will count the characters of “newString” including the whitespaces:

int newString = string.length();
System.out.println(“The Characters in the String including spaces: ” + newString);

Now, we will find the count of the characters of a string without spaces. For that, we will call the “replace()” method of the String class with the “length()” method. The replace() method accepts two parameters that will neglect the spaces from the string and returns the count of the characters using the length() method:

int newStrng = string. replace ( » » , «» ) . length ( ) ;
System . out . println ( «The Characters in the String without spaces: » + newStrng ) ;

The output shows the 21 as characters count, including spaces, while without spaces, the count of the character is 18:

Let’s check out the third method!

Method 3: Count Characters in a String in Java Using String.chars.count() Method

The “String.chars().count()” method returns the number of characters present in the string, with white spaces. Additionally, we will use the “filter()” method to count characters without spaces.

Example
In this method, we will count the characters of our “strng” String without spaces by utilizing the “String.chars.filter().count()” method:

Do you only want to count the numbers of a particular character occurrence? Check out the following section!

Method 4: Count Characters in a String in Java Using charAt() Method

In a Java program, the “charAt()” method is used if you want to find the occurrence of a specific character in a string.

Example
In this example, we will check how many times the character “n” appears in the string. For this purpose, we will again use the same string that is used in the above example and create an integer type variable “chCount” initialized with “0”, and a character type variable named “findChar” initialized with character “n”:

Читайте также:  Print Me

Now, we will iterate the string until the full length of the string using “for” loop and match every character with “findChar” that is “n” and increment the count if the added condition is evaluated as true, otherwise move to the next iteration:

Lastly, print the character count:

System.out.println(«The Character ‘» +findChar+ «‘ in the String is ‘» +chCount+ «‘ times»);

The given output states that in the “strng” String, the “n” character occurred two times:

We compiled all the necessary information on how to count the characters in a string in Java.

Conclusion

To count characters in a string in Java, there are different methods: using for loop, charAt() method, String.chars.count() method, and the String.length() method. You can count characters from strings, including spaces, without spaces, and the occurrence of the specific character in a string by using these methods. This tutorial discussed the methods to count characters in a string in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Java Program to count letters in a String

Let’s say we have the following string, that has some letters and numbers.

Now loop through the length of this string and use the Character.isLetter() method. Within that, use the charAt() method to check for each character/ number in the string.

We have set a count variable above to get the length of the letters in the string.

Here’s the complete example.

Example

public class Demo < public static void main(String []args) < String str = "9as78"; int count = 0; System.out.println("String: "+str); for (int i = 0; i < str.length(); i++) < if (Character.isLetter(str.charAt(i))) count++; >System.out.println("Letters: "+count); > >

Output

Let us see another example.

Example

public class Demo < public static void main(String []args) < String str = "amit"; int count = 0; System.out.println("String: "+str); for (int i = 0; i < str.length(); i++) < if (Character.isLetter(str.charAt(i))) count++; >System.out.println("Letters: "+count); > >

Output

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • Java Program to count vowels in a string
  • Java program to count words in a given string
  • Java Program to count all vowels in a string
  • Java program to count occurrences of a word in string
  • Java Program to count the number of words in a String
  • How to get the maximum count of repeated letters in a string? JavaScript
  • Java Program to get random letters
  • How to find If a given String contains only letters in Java?
  • Java program to count upper and lower case characters in a given string
  • C# Program to count vowels in a string
  • Java program to count the occurrence of each character in a string using Hashmap
  • Python program to calculate the number of digits and letters in a string
  • C# program to Count words in a given string
  • Python program to Count words in a given string?
  • How to count the number characters in a Java string?

Источник

Count number of each char in a String

I know there is a simpler way of doing this, but I just really can’t think of it right now. Can you please help me out?

String sample = "hello world"; char arraysample[] = sample.toCharArray(); int length = sample.length(); //count the number of each letter in the string int acount = 0; int bcount = 0; int ccount = 0; int dcount = 0; int ecount = 0; int fcount = 0; int gcount = 0; int hcount = 0; int icount = 0; int jcount = 0; int kcount = 0; int lcount = 0; int mcount = 0; int ncount = 0; int ocount = 0; int pcount = 0; int qcount = 0; int rcount = 0; int scount = 0; int tcount = 0; int ucount = 0; int vcount = 0; int wcount = 0; int xcount = 0; int ycount = 0; int zcount = 0; for(int i = 0; i < length; i++) < char c = arraysample[i]; switch (c) < case 'a': acount++; break; case 'b': bcount++; break; case 'c': ccount++; break; case 'd': dcount++; break; case 'e': ecount++; break; case 'f': fcount++; break; case 'g': gcount++; break; case 'h': hcount++; break; case 'i': icount++; break; case 'j': jcount++; break; case 'k': kcount++; break; case 'l': lcount++; break; case 'm': mcount++; break; case 'n': ncount++; break; case 'o': ocount++; break; case 'p': pcount++; break; case 'q': qcount++; break; case 'r': rcount++; break; case 's': scount++; break; case 't': tcount++; break; case 'u': ucount++; break; case 'v': vcount++; break; case 'w': wcount++; break; case 'x': xcount++; break; case 'y': ycount++; break; case 'z': zcount++; break; >> System.out.println ("There are " +hcount+" h's in here "); System.out.println ("There are " +ocount+" o's in here "); 

Источник

Читайте также:  Python file copy move

Java: Count the letters, spaces, numbers and other characters of an input string

Write a Java program to count letters, spaces, numbers and other characters in an input string.

Test Data:
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33

Pictorial Presentation:

Java: Count the letters, spaces, numbers and other characters of an input string

Sample Solution:

import java.util.Scanner; public class Exercise38 < public static void main(String[] args) < String test = "Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33"; count(test); >public static void count(String x) < char[] ch = x.toCharArray(); int letter = 0; int space = 0; int num = 0; int other = 0; for(int i = 0; i < x.length(); i++)< if(Character.isLetter(ch[i]))< letter ++ ; >else if(Character.isDigit(ch[i])) < num ++ ; >else if(Character.isSpaceChar(ch[i])) < space ++ ; >else < other ++; >> System.out.println("The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33"); System.out.println("letter: " + letter); System.out.println("space: " + space); System.out.println("number: " + num); System.out.println("other: " + other); > > 
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33 letter: 23 space: 9 number: 10 other: 6

Flowchart: Java exercises: Count the letters, spaces, numbers and other characters of an input string

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource’s quiz.

Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How to convert float to int with Java?

Using Math.round() will round the float to the nearest integer.

  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Оцените статью