如何使用 c++ 中的 int* count(const string&s)计算字符串中每个数字的出现次数?
How do I count the occurences of each digit in a string using int* count (const string& s) in c++?
注意:我不能使用算法库中的地图或任何东西
我只有主函数,但完全不知道我应该如何编写函数
#include <string>
#include <iostream>
using namespace std;
int* count(const string& s);
int main() {
string userinput = "random word";
int *counts = count(userinput);
for (int i = 0; i < 11; i++) {
cout << "Letter " << i << " occured " << counts[i] << " times.";
}
system("pause");
return 0;
}
int* count(const string& s) {
for (int i = 0; i < 11; i++)
{
return s[i];
}
}
int* count 函数不正确,无法编译。如何编写一个将返回出现的函数?
如何使用
int* 计算字符串中每个数字的出现次数 C++中的count(const string&s(?注意:我不能使用地图或任何内容 算法库
需要注意的几点:
总是坏主意,如果我们不遵循最简单的方法来解决问题,那么教师是这种情况的推动者是可悲的。
不需要int*
,它不会以任何方式提供您的解决方案。如果是数组来计算字符串的字符数,那么它可能是有意义的(即指向数组的指针(。尽量避免与
using namespace std;
一起练习
如果不允许使用std::map<char, int>
,您可以执行以下操作(替代解决方案的提示(。
为了更好地理解,我已经对此进行了评论。
#include <string>
#include <iostream>
#include <cstddef>
// consider 128 ASCII decimal and their coresponding character codes
int charASCIIArray[128] = {0};
void count_char(const std::string& s)
{
for(const auto& it: s)
{
if(('A' <= it && it <= 'Z') || // if char between (A,B,....,Z) or
('a' <= it && it <= 'z') ) // between (a,b,....,z)
charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
}
}
int main()
{
std::string userinput = "random words WITH *- aLl";
count_char(userinput);
for(std::size_t index = 0; index < 128; ++index)
if(charASCIIArray[index] != 0)
std::cout << "Letter " << static_cast<char>(index) // convert back to char
<< " occured " << charASCIIArray[index] << " times.n";
return 0;
}
在此处观看真人表演:https://www.ideone.com/uFG2HJ
Letter H occured 1 times.
Letter I occured 1 times.
Letter L occured 1 times.
Letter T occured 1 times.
Letter W occured 1 times.
Letter a occured 2 times.
Letter d occured 2 times.
Letter l occured 1 times.
Letter m occured 1 times.
Letter n occured 1 times.
Letter o occured 2 times.
Letter r occured 2 times.
Letter s occured 1 times.
Letter w occured 1 times.
更新
受@Fei Xiang评论的启发,这里有两个过度工程化的解决方案:
首先,返回指向动态数组的指针(这将满足实际 问题要求(:https://www.ideone.com/ouHqK4
#include <string>
#include <iostream>
#include <cstddef>
int* count_char(const std::string& s)
{
// consider 128 ASCII decimal and their coresponding character codes
int *charASCIIArray = new int[128]{0};
for(const auto& it: s)
{
if(('A' <= it && it <= 'Z') || // if char between (A,B,....,Z) or
('a' <= it && it <= 'z') ) // between (a,b,....,z)
charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
}
return charASCIIArray;
}
int main()
{
std::string userinput = "random words WITH *- aLl";
int *charASCIIArray = count_char(userinput);
for(std::size_t index = 0; index < 128; ++index)
if(charASCIIArray[index] != 0)
std::cout << "Letter " << static_cast<char>(index) // convert back to char
<< " occured " << charASCIIArray[index] << " times.n";
delete[] charASCIIArray;
return 0;
}
其次使用智能指针(std::unique_ptr
(:https://www.ideone.com/dfc62J
#include <string>
#include <iostream>
#include <cstddef>
#include <memory>
#include <utility>
std::unique_ptr<int[]> count_char(const std::string& s)
{
// consider 128 ASCII decimal and their coresponding character codes
std::unique_ptr<int[]> charASCIIArray = std::unique_ptr<int[]>(new int[128]{0});
for (const auto& it : s)
{
if (('A' <= it && it <= 'Z') || // if char between (A,B,....,Z) or
('a' <= it && it <= 'z')) // between (a,b,....,z)
charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
}
return std::move(charASCIIArray);
}
int main()
{
std::string userinput = "random words WITH *- aLl";
std::unique_ptr<int[]> charASCIIArray = count_char(userinput);
for (std::size_t index = 0; index < 128; ++index)
if (charASCIIArray[index] != 0)
std::cout << "Letter " << static_cast<char>(index) // convert back to char
<< " occured " << charASCIIArray[index] << " times.n";
return 0;
}
不太确定你想要这个答案,但我希望它有所帮助 您可以将其分成单独的功能,但我一次性完成了
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userInput = "random word";
int fCharacter[256];
for (int i = 0; i <= 255; i++)
{
fCharacter[i] = 0;
}
for (int i = 0; i < userInput.length(); i++)
{
fCharacter[userInput[i]]++;
}
cout << "The character changes are as follows" << endl;
for (int i = 0; i <= 255; i++)
{
if (fCharacter[i] != 0)
{
cout << (char)i << endl;
}
}
system("pause");
return 0;
}
相关文章:
- 生成随机长度的数字字符串
- 如何在c ++中将大数字字符串转换为整数?
- 将非数字字符串存储为二进制整数
- Flex 词法分析器规则,对包含连字符和斜杠的字母数字字符串具有积极的前瞻断言
- 如何在C++中将数字字符串拆分为数组
- 总结数字字符串输入的最佳方法
- 如何将输入的数字字符串转换为 int 数组
- 如何获得两个数字字符串的总和-C
- 使用 C++ 替换 txt 文件中的文本数字字符串
- 如何有效地将数字字符串值分配给整数
- String Stream in C++ 解析单词和数字字符串
- c++将大型数字字符串映射到数字字符串范围
- 获取用数字字符串表示的树中的所有直接父级/子级
- 如何从 char 数组中提取数字字符串成员
- 比较两个字母数字字符串
- 有没有一种方法可以设置浮点变量=某个数字*字符串中的变量
- 使用矢量对字母数字字符串进行排序
- 正在分析未知类型的数字字符串
- 如何在c++中从一个科学数字字符串中修剪后面的零
- 无法将字符串数组中的数字字符串转换为浮点值