开发者

counting characters in char array

i wrote following code to count occurance of characters in given char array

#include <iostream>
#include <string>
using namespace std;
int main(){

char f[]={'a','a','b','c','c','d','d','f','e','e'};
int n=sizeof(f)/sizeof(char);
char max=f[0];
for (int i=1;i<n;i++)
{
    if(f[i]>max)
    {
        max=f[i];
    }
}

int m=(int)(max)+1;
int  *b=new int [m];

for (int i=0;i<n;i++)
{
    b[(int)(f[i])]++;
}

for (int i=0;i<m;i++)
{
    if(b[i]!=0)
    {
    cout<<(char)(i)<<" occurs  "<<b[i]<< "times "<<endl;   
    }      
}

return 0;
}

but it shows very bad result

 occurs  -842150451times
☺ oc开发者_如何学运维curs  -842150451times
☻ occurs  -842150451times
♥ occurs  -842150451times
♦ occurs  -842150451times
♣ occurs  -842150451times
♠ occurs  -842150451times
 occurs  -842150451times
 occurs  -842150451times
         occurs  -84215045

 occurs  -842150451times
♂ occurs  -842150451times
♀ occurs  -842150451times
 occurs  -842150451times
♫ occurs  -842150451times
☼ occurs  -842150451times
► occurs  -842150451times
◄ occurs  -842150451times
↕ occurs  -842150451times
‼ occurs  -842150451times
¶ occurs  -842150451times
§ occurs  -842150451times
▬ occurs  -842150451times
↨ occurs  -842150451times
↑ occurs  -842150451times
↓ occurs  -842150451times
→ occurs  -842150451times
← occurs  -842150451times
∟ occurs  -842150451times
↔ occurs  -842150451times
▲ occurs  -842150451times
▼ occurs  -842150451times
  occurs  -842150451times
! occurs  -842150451times
" occurs  -842150451times
# occurs  -842150451times
$ occurs  -842150451times
% occurs  -842150451times
& occurs  -842150451times
' occurs  -842150451times
( occurs  -842150451times
) occurs  -842150451times
* occurs  -842150451times
+ occurs  -842150451times
, occurs  -842150451times
- occurs  -842150451times
. occurs  -842150451times
/ occurs  -842150451times
0 occurs  -842150451times
1 occurs  -842150451times
2 occurs  -842150451times
3 occurs  -842150451times
4 occurs  -842150451times
5 occurs  -842150451times
6 occurs  -842150451times
7 occurs  -842150451times
8 occurs  -842150451times
9 occurs  -842150451times
: occurs  -842150451times
; occurs  -842150451times
< occurs  -842150451times
= occurs  -842150451times
> occurs  -842150451times
? occurs  -842150451times
@ occurs  -842150451times
A occurs  -842150451times
B occurs  -842150451times
C occurs  -842150451times
D occurs  -842150451times
E occurs  -842150451times
F occurs  -842150451times
G occurs  -842150451times
H occurs  -842150451times
I occurs  -842150451times
J occurs  -842150451times
K occurs  -842150451times
L occurs  -842150451times
M occurs  -842150451times
N occurs  -842150451times
O occurs  -842150451times
P occurs  -842150451times
Q occurs  -842150451times
R occurs  -842150451times
S occurs  -842150451times
T occurs  -842150451times
U occurs  -842150451times
V occurs  -842150451times
W occurs  -842150451times
X occurs  -842150451times
Y occurs  -842150451times
Z occurs  -842150451times
[ occurs  -842150451times
\ occurs  -842150451times
] occurs  -842150451times
^ occurs  -842150451times
_ occurs  -842150451times
` occurs  -842150451times
a occurs  -842150449times
b occurs  -842150450times
c occurs  -842150449times
d occurs  -842150449times
e occurs  -842150449times
f occurs  -842150450times

please help me what is wrong?


You have to initalize the "malloc"ed (new) area to 0, it contains garbage.


As others have said the mistake you did, I will give you another way of doing. Since there are 256 ASCII characters, just allocated a character array of that size.

char mapCount[256];
std::fill(mapCount, mapCount+256, 0); // std::fill is from <algorithm> header

Now as you did earlier, just traverse through the input sequence and the increase the corresponding mapCount value. i.e.,

++mapCount[(int)inputSequence[i]];

Finally output mapCount indexes whose value is greater than 0. Converting the index to char will give you the actual character. As @Kerrek SB suggested, there is a notion of key association with a value. You can use the associative container std::map for this task though.


Well, your b array is not initialized ...

my 2 cents


Allocated buffer should be initialized to zero

int *b=new int [m];
memset(b, 0, sizeof(int) * m);


Not sure I want to dig through this code, but since you tagged this as C++, I recommend The C++ Way of doing this:

#include <map>
#include <iostream>

std::map<char, unsigned int> histogram;

// Counting
for (char * it = f; it != f + sizeof(f); ++it)
  ++histogram[*it];

// Reporting
for (std::map<char, unsigned int>::const_iterator it = histogram.begin(); it != histogram.end(); ++it)
  std::cout << "Character '" << it->first << "' appears " << it->second << " times.\n";

There's no manual memory allocation, no forgetting to clean up, and it's fairly self-descriptive. (If the array f comes as a pointer without size information, you have to pass the size information separately and use that in place of sizeof(f).)


The actual "error" is that you are not initialising the memory you have allocated to zero, and new[] won't do that for you.

Aside from using new[] you are not actually writing any C++ at all, it is all C. Why not use vector? That will also initialise the values to 0 for you.

// at the top

#include <vector>

then

std::vector<int> b[m];

Finally use meaningful names for identifiers rather than single letters.


There's quite a few problems here:

What are you trying to do here?

char f[]={'a','a','b','c','c','d','d','f','e','e'};
int n=sizeof(f)/sizeof(char);
char max=f[0];
for (int i=1;i<n;i++)
{
    if(f[i]>max)
    {
       max=f[i];
    }
}

This code when finished will have assigned max the value of the highest character in the array, in this case e.

And here?

int m=(int)(max)+1;
//cout<<m;
int  *b=new int [m];

This code will allocate a new array of integers, of size m, which will be 'e' + 1. Almost certainly not what you want. You want this to be the length of your string (n).

As others have already stated, b will contain uninitialized values which will also cause some unwanted behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜