Answers for "how to determine the number of characters in a string"

2

counting the number of characters in a string java

public static void main(String[] args) {
		int wordCount = 0;
		String word = "hill";
		for (char letter = 'a'; letter <= 'z'; letter++) {
			for (int i = 0; i < word.length(); i++) {
				if (word.charAt(i) == letter) {
					wordCount++;
				}
			}
			if (wordCount > 0) {
				System.out.println(letter + "=" + wordCount);
				wordCount = 0;
			}
		}
	}
Posted by: Guest on January-23-2020
0

count characters in string

# count characters iterative
def count_chars(s)
  count = Hash.new(0)
  s.split("").each do |x|
    count[x] += 1
  end
  count
end

# count characters functional
s.chars.map {|c| [c, s.chars.count(c)] }.to_h
Posted by: Guest on May-21-2021

Code answers related to "how to determine the number of characters in a string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language