通读文件中的字符并计算有多少个字母 [c++]
Read through chars in a file and count how many individual letters there are [c++]
因此用户将输入一个包含任何文本的文件,程序需要通读每个字符并计算每个字符的数量(仅限大写和小写字母)。所以,可能有 50 个 A,23 个 B,等等...
这是我目前在 main.cc 中的内容:
char character;
int i = 65; //this is the iterator to go through upper and lowercase letters
int count = 0; //counts number of characters and resets when exiting the loop and after using cout
ifstream file(filename); //filename is a string the user inputs
while (i != 0) {
while (file >> character) {
int a = character;
cout << a << endl; //testing: outputs the correct number for the letter
if (i == a) { //but for some reason this part isn't working?
count++;
}
}
cout << count << endl; //this outputs 0 every time
count = 0;
i++;
if (i == 91) i = 97; //switch to lower case
if (i == 123) i = 0; //exit loop
}
感谢您的帮助!谢谢:)
这是使用地图的理想场所
从文件中读取一个字符
file >> character;
映射位置的增量
if( isalpha(character) ) { myMap[character]++; }
最后,您可以遍历所有地图条目并将它们全部打印出来。
for(map<char, int >::const_iterator it = myMap.begin(); it != myMap.end(); ++it)
{
std::cout << "The character " << it->first << " was found " << it->second<< " times." << std::endl;
}
让我们假设文本是 ASCII 或扩展 ASCII 格式的,因此最多可能有 256 个字符。
您可以使用数组来保存给定字符出现的次数。每个插槽对应一个字符;相反,字符可以用作数组的索引。
示例:
unsigned int MAXIMUM_CHAR_VALUES = 256U;
unsigned int occurrences[MAXIMUM_CHAR_VALUES] = {0};
char c;
while (my_text_file >> c)
{
++occurrences[c];
}
// Print them out
for (unsigned int i = 0; i < MAXIMUM_CHAR_VALUES; ++i)
{
if (!isprint(i))
{
cout << "0x" << hex << i;
}
else
{
c = static_cast<char>(i);
cout << c;
}
cout << ": " << occurrences[i] << "\n";
}
如果必须使用"nodes",您可以将数组更改为节点数组。
还有其他结构可以使用,例如二叉树。
多亏了其他答案,我想出了这种方法……到目前为止效果很好。谢谢大家!
char character;
int a[256] = {0};
int i = 65;
ifstream file(filename);
while (file >> character) {
a[character]++;
}
while (i != 0) {
character = i;
cout << "There are " << a[i] << " of " << character << endl;
i++;
if (i == 91) i = 97;
if (i == 123) i = 0;
}
因此用户将输入一个包含任何文本的文件,程序需要通读每个字符并计算每个字符的数量(仅限大写和小写字母)。所以,可能有 50 个 A,23 个 B,等等...
这是我目前在 main.cc 中的内容:
char character;
int i = 65; //this is the iterator to go through upper and lowercase letters
int count = 0; //counts number of characters and resets when exiting the loop and after using cout
ifstream file(filename); //filename is a string the user inputs
while (i != 0) {
while (file >> character) {
int a = character;
cout << a << endl; //testing: outputs the correct number for the letter
if (i == a) { //but for some reason this part isn't working?
count++;
}
}
cout << count << endl; //this outputs 0 every time
count = 0;
i++;
if (i == 91) i = 97; //switch to lower case
if (i == 123) i = 0; //exit loop
}
感谢您的帮助!谢谢:)
这是使用地图的理想场所
从文件中读取一个字符
file >> character;
映射位置的增量
if( isalpha(character) ) { myMap[character]++; }
最后,您可以遍历所有地图条目并将它们全部打印出来。
for(map<char, int >::const_iterator it = myMap.begin(); it != myMap.end(); ++it)
{
std::cout << "The character " << it->first << " was found " << it->second<< " times." << std::endl;
}
让我们假设文本是 ASCII 或扩展 ASCII 格式的,因此最多可能有 256 个字符。
您可以使用数组来保存给定字符出现的次数。每个插槽对应一个字符;相反,字符可以用作数组的索引。
示例:
unsigned int MAXIMUM_CHAR_VALUES = 256U;
unsigned int occurrences[MAXIMUM_CHAR_VALUES] = {0};
char c;
while (my_text_file >> c)
{
++occurrences[c];
}
// Print them out
for (unsigned int i = 0; i < MAXIMUM_CHAR_VALUES; ++i)
{
if (!isprint(i))
{
cout << "0x" << hex << i;
}
else
{
c = static_cast<char>(i);
cout << c;
}
cout << ": " << occurrences[i] << "\n";
}
如果必须使用"nodes",您可以将数组更改为节点数组。
还有其他结构可以使用,例如二叉树。
多亏了其他答案,我想出了这种方法……到目前为止效果很好。谢谢大家!
char character;
int a[256] = {0};
int i = 65;
ifstream file(filename);
while (file >> character) {
a[character]++;
}
while (i != 0) {
character = i;
cout << "There are " << a[i] << " of " << character << endl;
i++;
if (i == 91) i = 97;
if (i == 123) i = 0;
}