简体   繁体   English

将字符串转换为char数组

[英]Convert a string into a char array

New to C++ and So here is part of a project I'm working on, taking a string and printing the most commonly used number along with how many times it was used. C ++的新手,所以这是我正在研究的项目的一部分,它取一个字符串并打印最常用的数字以及使用了多少次。 i thought this was right, but for some reason my char array wont be read in. any tips or suggestions on how to fix? 我认为这是正确的,但是由于某种原因,我的char数组将无法读取。有关如何修复的任何提示或建议?

#include <string>
#include <iostream>

using namespace std;

char getMostFreqLetter(string ss);

int main() {
    string s; //initilizing a variable for string s
    s = ("What is the most common letter in this string "); // giving s a string

    getMostFreqLetter(s); // caling the function to print out the most freq Letter

    return 0;
}

char getMostFreqLetter(string ss) {
    int max, index, i = 0;
    int array[255] = {0};
    char letters[];

    // convert all letters to lowercase to make counting letters non case sensative
    for (int i = 0; i < ss.length(); i ++){
        ss[i] = tolower(ss[i]);
    }

    //read each letter into
    for (int i = 0; i < ss.length(); i ++){
        ++array[letters[i]];
    }
    //

    max = array[0];
    index = 0;
    for (int i = 0; i < ss.length(); i ++){
        if( array[i] > max)
        {
            max = array[i];
            index = i;
        }
    }

    return 0;
}

If you are not considering white space as letter. 如果您不将空格视为字母。

Then more efficient way could have been 那本来可以更有效的方法

vector<int> count(26,0);
for (int i = 0; i < s.length(); i++) {
    int range = to_lower(s[i])-'a';
    if ( range >= 0 && range < 26)
        count[range]++;
}

// Now you can do fix the max while iterating over count;

Use string::c_str() . 使用string :: c_str() It converts a string to a character array. 它将字符串转换为字符数组。

You have a few errors in your code. 您的代码中有一些错误。

Firstly, the array of chars letters is completely unused. 首先,字符的阵列letters是完全未使用。 You should disregard it and iterate over the string ss instead which is what I think you intended to do. 您应该忽略它,而是遍历字符串ss ,这是我认为您打算做的事情。

This would change your second for loop from ++array[letters[i]]; 这将从++array[letters[i]];更改第二个for循环++array[letters[i]]; to ++array[ss[i]]; ++array[ss[i]]; .

Secondly, your last for loop is buggy. 其次,您的最后一个for循环是越野车。 You are using i as the index to look for the frequency in array whereas you need to use the ascii value of the character (ss[i]) instead. 您使用i作为索引来查找数组中的频率,而您需要使用字符的ascii值(ss [i])。 Here is a fixed version with comments: 这是带注释的固定版本:

    index = ss[0];
    max = array[index];
    for (int i = 0; i < ss.length(); i ++){
        if(!isspace(ss[i]) && array[ss[i]] > max)
        {
            max = array[ss[i]];   // you intended to use the ascii values of the characters in s to mark their place in array. In you code, you use i which is the just the index of the character in s as opposed to the ascii value of that character. Hence you need to use array[ss[i]].
            index = ss[i];

        }
    }
    return index;

Once you make the above changes you get the following output when run on your string: 进行以上更改后,在字符串上运行时将获得以下输出:

Most freq character: t

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

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