【问题标题】:Counting letters by comparing string to array通过比较字符串和数组来计算字母
【发布时间】:2015-10-16 11:26:11
【问题描述】:

我正在开发一个函数,该函数将用户输入作为 main 的引用,并将其与存储字母表的字符串数组进行比较。我的目标是让函数计算 user_string 中每个字母出现的次数。这是我到目前为止得到的:

int letter_counter(string user_Text_Ref)
{
    string alfa_arr[LETTERS] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    int antal_arr[LETTERS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int i, j;

    for(i = 0; i < LETTERS; i++)
    {
        for(j = 0;j < sizeof(user_Text_Ref) ; j++)
        {
            if(alfa_arr[i] == user_Text_Ref(j)) // Here is my problem.
            {
                antal_arr[i] += 1;
                cout << alfa_arr[i] << " : " << antal_arr[i] << endl;
            }
            else
            {
                continue;
            }
        }
    }
return antal_arr;
}

所以快速总结一下这是什么。函数 letter_counter 从 main 引用用户提交的字符串作为参数。 LETTERS 是一个设置为 26 的 const int。 alfa_arr 包含所有小写字母 a-z 并且 antal_arr 包含 与字母对应的数字,用于记录所有的出现次数 这些信。 第一个 for 循环遍历所有字母,第二个循环遍历字符串 user_Text_Ref,我很困惑。

我尝试了几种比较 alfa_arr[i] 和 user_Text_Ref 的方法(==,strcmp 和这些的变体,还有一些我不记得了) 我收到一个我不理解的错误(我正在使用代码块):

error: no match for call to ‘(std::string {aka std::basic_string<char>}) (int&)’|

这是什么意思?

在这种情况下比较两个字符串的最佳方法是什么?

(你能用字符串和整数创建一个多维数组吗?)

我对 c++ 还是很陌生,感谢任何帮助或建议。 感谢您花时间阅读我的问题。

干杯!

【问题讨论】:

  • 您可能更喜欢 int antal_arr[LETTERS] = {}; 来进行零初始化
  • 你可以用user_Text_Ref[j]得到string的第i个字符
  • sizeof(std::string)?
  • Maps 将字母存储为键并将出现次数存储为值是一个更好的选择
  • 为了解释@LogicStuff 注释,你得到std::string 实例的大小和user_Text_Ref.size()

标签: c++ arrays string reference


【解决方案1】:

该错误表示您正在尝试将字符串用作函数。要从中获取单个字符,请使用[],而不是()。这样你就可以索引字符串中的单个字符。

if(alfa_arr[i] == user_Text_Ref[j])

其次,你有一个字符串数组,用于alfa-arr 中的字符。您很可能希望在其中存储单个字符并将其与从字符串中迭代的字符进行比较。

char alfa_arr[LETTERS] = { 'a', 'b', ... };

第三,要获得字符串的长度,使用user_Text_Ref.length()sizeof 在这种情况下只会给你分配给该对象的内存大小。

【讨论】:

  • 这个答案清除了一切。谢谢你萨米!我真的很感激指导。现在我明白了为什么当我尝试将 alfa_arr 设置为字符时它会抛出错误。因为我使用了双引号,所以它把它当作一个带有一个字符和一个空终止符的字符串文字。我将其更改为单引号并使其工作。现在我只需要找出返回数组的正确方法。
【解决方案2】:

应该是

user_Text_Ref[j]

另外请注意,您可以通过简单地从该字母中减去 'a' 来找出字母的编号:

for(j = 0;j < user_Text_Ref.length(); j++) { // sizeof is also a problem here
    if (user_Text_Ref[j]>='a' && user_Text_Ref[j]<='z') { // if this is a letter
        pos =  user_Text_Ref[j] - 'a'; // find its number but sbtracting 'a' code from its code
        antal_arr[pos] ++;
        cout << user_Text_Ref[j] << " : " << antal_arr[pos] << endl;
    }
}

所以你根本不需要alfa_arr 数组。

【讨论】:

    【解决方案3】:

    您可以使用此代码实现您的目标...

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int LETTERS = 26;
    
    void letter_counter(string str, int* p) {
    
        // fill up alphabet array
        char alfa_arr[LETTERS] = { 0 };
        for (int i = 0; i < LETTERS; i++) {
            alfa_arr[i] = static_cast<char>(i + 97);
        }
    
        // convert string to lowercase
        transform(str.begin(), str.end(), str.begin(), ::tolower);
    
        // fill up occurrences array
        for (int i = 0; i < LETTERS; i++) {
            *p++ = count(str.begin(), str.end(), alfa_arr[i]);
        }
    }
    
    int main() {
    
        int antal_arr[LETTERS] = { 0 };
        int* p = nullptr;
        string str{"This program counts letter OCCURRENCES in TEXT despite CASE"};  
    
        // fill occurrences array
        p = &antal_arr[0];
        letter_counter(str, p);
    
        // show results
        for (int i = 0; i < LETTERS; i++) {
            cout << antal_arr[i] << endl;
        }
    
        return 0;
    }
    

    请注意,letter_counter 不返回 int 类型。相反,它会填充一个外部的 int 数组。

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2021-03-18
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多