在C++中查找三个字符串中所有字符的出现次数
Finding number of occurences of all characters in three strings in C++
我不明白为什么这个程序不工作。
我需要通过任何方法找到三个字符串中所有字符的出现次数。
我用了计数法,但如果你们能帮我找到函数,那个会更好。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string line[3];
int count[3];
cout << "Enter three lines of text...nn";
cin >> line[0];
cin >> line[1];
cin >> line[2];
int i;
for(char j='a'; j<=26; j++) {
for(i=0; i<3; i++)
count[i] = std::count(line[i].begin(), line[i].end(), j);
cout << "n" << j << "t" << ":" << "t" << count[i];
}
system ("pause");
return 0;
}
26不是字母,(char)26
通常小于'a'
,因此循环不会执行。尝试char j='a';j<='z';j++
我会选择这样的东西:
#include <map>
#include <string>
#include <iostream>
int main()
{
// Given 3 strings...
std::string s1 = "Hello";
std::string s2 = "Cruel";
std::string s3 = "World";
//===============================================
// THESE 2 LINES ARE ALL YOU NEED FOR COUNTING
std::map<char, int> countMap;
for (char c : (s1 + s2 + s3)) { countMap[c]++; }
//===============================================
// Print the output. This is if you do not care about
// characters that do not appear at all.
for (auto const& e : countMap)
{
std::cout << e.first << ": " << e.second << std::endl;
}
// Print the output. This is if you DO care about
// characters that do not appear at all.
for (char c = ' '; c <= '~'; c++)
{
std::cout << c << ": " << countMap[c] << std::endl;
}
}
http://www.asciitable.com/
小写字母"a"为96。它小于26,这就是为什么你的循环没有执行的原因。尝试:
for (char j = 'a'; j <= 'z'; j++)
这将只计算小写字符。如果你想出现小写和大写,你可以这样做:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string line[3];
cout << "Enter three lines of text...nn";
cin >> line[0];
cin >> line[1];
cin >> line[2];
for(char j='a';j<='z';j++)
{
int upper_sum = 0;
int lower_sum = 0;
for(int i=0;i<3;i++)
{
lower_sum += std::count(line[i].begin(),line[i].end(),j);
upper_sum += std::count(line[i].begin(),line[i].end(),j - 32); //32 = 'a' - 'A'
}
cout<<"n"<<j<<"t"<<":"<<"t"<<lower_sum;
cout<<"n"<<(char)(j - 32)<<"t"<<":"<<"t"<<upper_sum;
}
return 0;
}
以下是您的程序的正确版本:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string line[3];
int count[3];
/*cout<<"Enter three lines of text...nn";
cin>>line[0];
cin>>line[1];
cin>>line[2];*/
line[0] = "aaaaabbbbbbbbbbb";
line[1] = "ccccccddddddddddd";
line[2] = "kkkkkkkkkkkkk";
int i;
for(char j='a';j<= 'z';j++) // You can loop through the alphabet, but you need to go 'a' to 'z' and not 26
{
for(i=0;i<3;i++)
{ // You were missing these braces, you need them if your loop contains multiple lines
count[i]= std::count(line[i].begin(),line[i].end(),j);
cout<<"n"<<j<<"t"<<":"<<"t"<<count[i];
}
}
system ("pause");
return 0;
}
它在这里发挥作用。
int i;
for(char j='a'; j<=26; j++) {
for(i=0; i<3; i++)
count[i] = std::count(line[i].begin(), line[i].end(), j);
cout << "n" << j << "t" << ":" << "t" << count[i];
}
您的cout
行不在内部for
循环中。
内循环结束后,i
为3
。然后用超出数组边界的count[i]
调用cout
。未定义的行为,如果它崩溃了,你就很幸运了。
你的问题是i
。声明的地方意味着它仍然存在,cout
行可以引用它。如果你把声明移到循环初始化器,你会发现下一个错误:
for(int i=0; i<3; i++)
count[i] = std::count(line[i].begin(), line[i].end(), j);
cout << "n" << j << "t" << ":" << "t" << count[i];
最后一行不会编译,因为i
在循环结束时超出了作用域。我认为您错误地认为cout
是循环的一部分,部分原因是代码的格式不好,其次是i
是在更大的范围内声明的。
要更正此问题,请使用{
和}
为for
循环创建一个块。这将使i
保持在块中所有语句的作用域中,并为循环的每次迭代重复这些语句。
for(int i=0; i<3; i++) {
count[i] = std::count(line[i].begin(), line[i].end(), j);
cout << "n" << j << "t" << ":" << "t" << count[i];
}
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 我有三个 getline,但是一旦编译,输入就太多了
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 有没有更好的方法对C++中的三个整数进行排序?
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 为什么 getch 在按下函数或箭头键时返回三个值?
- 使用三个数字比较器进行排序
- 异常处理:如果用户输入不是三个特定字符之一
- 以三个无符号字符为关键字的无序映射
- 如何避免 CFile查找文件扩展名超过三个字符的误报
- C++从文件中读取会放置三个奇怪的字符
- 为什么我的字符串只接受三个字符
- 如何将字符串的三个字符放入数组的一个空间中
- 从每行开始收集前三个字符
- 连接三个字符数组
- 在C++中查找三个字符串中所有字符的出现次数
- 为什么我在尝试输出UTF-8字符时会得到三个不同的数字