简体   繁体   English

在不使用库的情况下计算字符串中字母出现的次数

[英]Counting number of times a letter occurs in a string without using libraries

My professor says I can't use libraries and stuff. 我的教授说我不能使用图书馆和东西。 I have my code with libraries: 我的库中有代码:

String phrase = keyboard.nextLine(); //Input.
int addr = phrase.length();
Map<Character, Integer> numChars = new HashMap<Character, Integer>(Math.min(addr, 26)); //there are 26 character in our alphabet. It makes a map and maps our the characters.

for (int i = 0; i < addr; ++i)//this area reads the string then, tells how many times each character appeared. Loops for each chracter.
{
  char charAt = phrase.charAt(i);

  if (!numChars.containsKey(charAt))
  {
    numChars.put(charAt, 1);
  }
  else if (numChars.containsKey(charAt))
  {
    numChars.put(charAt, 0);
  }
  else
  {
    numChars.put(charAt, numChars.get(charAt) + 1);
  }
}
  System.out.println(phrase);//outputs phrase written by user.
  System.out.println(numChars);//outputs results of the code above

// this code reads which one appeared the most.
 int FreqChar = 0;
 char frequentCh = ' ';

 for (int f = 0; f < phrase.length(); f++)
 {
    char poop = phrase.charAt(f);
    int banana = 0;
    for (int j = phrase.indexOf(poop); j != -1; j = phrase.indexOf(poop, j + 1))
    {
        frequentCh++;
    }
    if (banana > FreqChar)
    {
        FreqChar = banana;*/

Here is my program without the libraries so far. 这是我到目前为止没有库的程序。 I need help translating this into arrays. 我需要帮助将其转换为数组。

    import java.util.*;

  public class LetCount
  {

  public static final int NUMCHARS = 26; //26 chars in alphabet.

  // int addr(char ch) returns the equivalent integer address for the letter
  // given in ch, 'A' returns 1, 'Z' returns 26 and all other letters return
  // their corresponding position as well. felt this is important.

  public static int addr(char ch)
  {
    return (int) ch - (int) 'A' + 1;
  }
// Required method definitions for (1) analyzing each character in an input
// line to update the appropriate count; (2) determining most frequent letter; 
// (3) determining least frequent letter; and (4) printing final results
// should be defined here.

  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);  // for reading input

    int[] count = new int [NUMCHARS]; // count of characters

    String phrase = keyboard.nextLine(); //Input.
    int addr = phrase.length();

       for(char ch = 'A'; ch <= 'Z'; ch++)
        {

    }
  }

This is easier to put here than in comments, but it's not really an answer :) 这比在评论中更容易放在这里,但这并不是真正的答案:)

You've got a great start - rather than going through the letters and finding matches, go through the string and increment your letter counter each time you encounter a letter (yeah, that sounds weird in my head too). 您已经有了一个不错的开始-不用遍历字母并找到匹配项,而是每次遇到一个字母时都要遍历字符串并增加字母计数器(是的,这听起来也很奇怪)。

Convert the string to lower or upper case first, you only need to count the letter, not whether it's lower or upper based upon your existing code. 首先将字符串转换为小写或大写字母,您只需要计算字母,而无需根据现有代码将其大小写。

If you just have to count all the alphabets in a given String and store it in an array, try this: 如果您只需要计算给定String中的所有字母并将其存储在数组中,请尝试以下操作:

String str = phrase.toUpperCase();    
for(int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    int charPositionInArray = c - 'A';
    if(charPositionInArray < 26){
        count[charPositionInArray] += 1;
    }
}

Also, the index of an array starts from 0, so I assume you want the count for 'A' to be stored in count[0], ie, the first position in the array. 另外,数组的索引从0开始,因此我假设您希望将'A'的计数存储在count [0]中,即数组中的第一个位置。

Also, this code does nothing for any character that is not an alphabet. 此外,此代码对非字母的任何字符均不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM