How do I count the occurences of each digit in a string using int*
count (const string& s) in c++? Note: I can't use maps or anything in
the algorithm library
A few things to note:
Always bad idea, if we don't follow the simplest way of approaching a problem and it's sad that teachers are the promoters of such situations.
There is not need of a int*
. which does not provide your solution at any manner. If it would have been array to count the chars of your string, then it might make sense(i.e, pointer to the array).
Try to avoid practicing with using namespace std;
You can do something like as follows, if you are not allowed to use std::map<char, int>
(hint for your alternative solution).
I have commented on it, for better understanding.
#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;
}
See live action here: 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.
UPDATE
Inspired from @Fei Xiang's comment, here is two over engineered solutions:
Firstly, with returning pointer to a dynamic array(which will meet actual
question requirements): 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;
}
Secondly with smart pointer(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;
}