[英]Write a program to print each and every alphabet with how many occured in a user input
I have to write a program to accept a String as input, and as output I'll have to print each and every alphabetical letter, and how many times each occurred in the user input. 我必须编写一个程序来接受String作为输入,并且作为输出,我必须打印每个字母字母,以及每个字母在用户输入中出现了多少次。 There are some constraints: 有一些限制:
For example, with this input: 例如,使用以下输入:
abbbccccdddddzz abbbccccdddddzz
I would expect this output: 我期望这个输出:
a-1,z-2,b-3,c-4,d-5 A-1,Z-2,B-3,C-4,d-5
This is what I have so far: 这是我到目前为止的内容:
public static void isCountChar(String s) {
char c1[] = s.toCharArray();
int c3[] = new int[26];
for (int i = 0; i < c1.length; i++) {
char c = s.charAt(i);
c3[c - 'a']++;
}
for (int j = 0; j < c3.length; j++) {
if (c3[j] != 0) {
char c = (char) (j + 'a');
System.out.println("character is:" + c + " " + "count is: " + c3[j]);
}
}
}
But I don't know how to sort. 但是我不知道如何排序。
some sort: easy if not easiest to understand an coding 某种形式:容易,即使不是最不容易理解的编码
You loop from the first element to the end -1 : element K 您从第一个元素循环到结尾-1:元素K
compare element K and element K+1: if element K>element K+1, invert them 比较元素K和元素K + 1:如果元素K>元素K + 1,将它们求逆
continue loop 继续循环
if you made one change redo that ! 如果您进行了一次更改,请重做!
First of all a tip for your next question: The things you've stated in your comments would fit better as an edit to your question. 首先,提出下一个问题的技巧:您在评论中陈述的内容更适合作为对问题的编辑。 Try to clearly state what the current result is, and what the expected result should be. 尝试清楚说明当前结果是什么,以及预期结果应该是什么。
That being said, it was an interesting problem, because of the two constraints. 话虽如此,由于两个限制,这是一个有趣的问题。
HashMap
with character as keys, and int as values, and then the sorting would be easy. 如果这不是一个约束,我会建议使用字符作为键,将int作为值的HashMap
,然后排序就很容易了。 BubbleSort
which I agree with, but it wouldn't work with your current code because it would sort by alphabetic character
instead of output value
. 这里的大多数人都建议使用类似于BubbleSort
的排序方式,但我同意,但是它不适用于您当前的代码,因为它将按alphabetic character
而不是output value
排序。 With these two constraints it is probably best to fake key-value
pairing yourself by making both an keys
-array and values
-array, and sort them both at the same time (with something like a BubbleSort
-algorithm). 有这两个约束,最好使自己的key-value
对伪造出来,方法是同时创建keys
-array和values
-array,并同时对它们进行排序(使用BubbleSort
-algorithm之类的方法)。 Here is the code: 这是代码:
private static final int ALPHABET_SIZE = 26;
public static void isCountChar(String s)
{
// Convert input String to char-array (and uppercase to lowercase)
char[] array = s.toLowerCase().toCharArray();
// Fill the keys-array with the alphabet
char[] keys = new char[ALPHABET_SIZE];
for (int i = 0; i < ALPHABET_SIZE; i++)
{
keys[i] = (char)('a' + i);
}
// Count how much each char occurs in the input String
int[] values = new int[ALPHABET_SIZE];
for (char c : array)
{
values[c - 'a']++;
}
// Sort both the keys and values so the indexes stay the same
bubbleSort(keys, values);
// Print the output:
for (int j = 0; j < ALPHABET_SIZE; j++)
{
if (values[j] != 0)
{
System.out.println("character is: " + keys[j] + "; count is: " + values[j]);
}
}
}
private static void bubbleSort(char[] keys, int[] values)
{
// BUBBLESORT (copied from http://www.java-examples.com/java-bubble-sort-example and modified)
int n = values.length;
for(int i = 0; i < n; i++){
for(int j = 1; j < (n - i); j++){
if(values[j-1] > values[j]){
// Swap the elements:
int tempValue = values[j - 1];
values[j - 1] = values[j];
values[j] = tempValue;
char tempKey = keys[j - 1];
keys[j - 1] = keys[j];
keys[j] = tempKey;
}
}
}
}
Example usage: 用法示例:
public static void main (String[] args) throws java.lang.Exception
{
isCountChar("TestString");
}
Output: 输出:
character is: e; count is: 1
character is: g; count is: 1
character is: i; count is: 1
character is: n; count is: 1
character is: r; count is: 1
character is: s; count is: 2
character is: t; count is: 3
Here is a working ideone to see the input and output. 这是查看输入和输出的工作思路 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.