-2

I'm looking for simple way to find the amount of letters in a sentence. All I was finding during research were ways to find a specific letter, but not from all kinds.

How I do that?

What I currently have is:

  • sentence = the sentence I get from the main method
  • count = the number of letters I want give back to the main method

public static int countletters(String sentence) {
    // ....
    return(count);
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Eli
  • 31
  • 1
  • 2
  • 3

6 Answers6

2

You could manually parse the string and count number of characters like:

    for (index = 1 to string.length()) {
       if ((value.charAt(i) >= 'A' && value.charAt(i) <= 'Z') || (value.charAt(i) >= 'a' && value.charAt(i) <= 'z')) {
          count++;
       }
    }
    //return count
SMA
  • 36,381
  • 8
  • 49
  • 73
2

A way to do this could stripping every unwanted character from the String and then check it's length. This could look like this:

public static void main(String[] args) throws Exception {
    final String sentence = "   Hello, this is the 1st example sentence!";
    System.out.println(countletters(sentence));
}

public static int countletters(String sentence) {
    final String onlyLetters = sentence.replaceAll("[^\\p{L}]", "");
    return onlyLetters.length();
}

The stripped String looks like:

Hellothisisthestexamplesentence

And the length of it is 31.

This code uses String#replaceAll which accepts a Regular Expression and it uses the category \p{L} which matches every letter in a String. The construct [^...] inverts that, so it replaces every character which is not a letter with an empty String.

Regular Expressions can be expensive (for the performance) and if you are bound to have the best performance, you can try to use other methods, like iterating the String, but this solution has the much cleaner code. So if clean code counts more for you here, then feel free to use this.

Also mind that \\p{L} detects unicode letters, so this will also correctly treat letters from different alphabets, like cyrillic. Other solutions currently only support latin letters.

Tom
  • 16,842
  • 17
  • 45
  • 54
1

SMA's answer does the job, but it can be slightly improved:

public static int countLetters(String sentence) {
    int count = 0;
    for (int i = 0; i < sentence.length; i ++)
    {
        char c = Character.toUpperCase(value.charAt(i));
        if (c >= 'A' && c <= 'Z')
            count ++;
    }

    return count;
}
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
1

This is so much easy if you use lambda expression:

long count = sentence.chars().count();

working example here: ideone

funcoding
  • 741
  • 6
  • 11
  • Thanks for the explanation! I understand what you mean, but unfortunately this is something that is not being enforced very frequently. For, example, I was downvote once because I include a link on an answer. And I have seen many answers with links. I admit my answer was very short, I could explain a little more and I'll do my best next time! – funcoding Apr 11 '17 at 00:11
  • @AndreiGheorghiu Thanks again! One of my objective is to help others and learn from others. I will take your advice to heart and strive for making it better on my answers! – funcoding Apr 11 '17 at 00:18
0

use the .length() method to get the length of the string, the length is the amount of characters it contains without the nullterm

if you wish to avoid spaces do something like

String input = "The quick brown fox";

int count = 0;
for (int i=0; i<input.length(); i++) {
    if (input.charAt(i) != ' ') {
        ++count;
    }
}
System.out.println(count);

if you wish to avoid other white spaces use a regex, you can refer to this question for more details

Community
  • 1
  • 1
svarog
  • 9,477
  • 4
  • 61
  • 77
0
import java.util.Scanner;
public class Main {

public static void main(String[] args){
    Scanner sc=new Scanner(System.in);

    String str = sc.nextLine();
    int count = 0;

    for (int i = 0; i < str.length(); i++) {
     if (Character.isLetter(str.charAt(i)))
     count++;
  }
    System.out.println(count);
}
}