如何使用 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++?

本文关键字:数字 字符串 计算 int 中的 c++ 何使用 count string const      更新时间:2023-10-16

注意:我不能使用算法库中的地图或任何东西

我只有主函数,但完全不知道我应该如何编写函数

#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(?注意:我不能使用地图或任何内容 算法库

需要注意的几点:

  1. 总是坏主意,如果我们不遵循最简单的方法来解决问题,那么教师是这种情况的推动者是可悲的。

  2. 不需要
  3. int*,它不会以任何方式提供您的解决方案。如果是数组来计算字符串的字符数,那么它可能是有意义的(即指向数组的指针(。

  4. 尽量避免与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;
}