Text analyzer not generating the correct results.

Greetings. I'm trying to make a simple program that takes a string, counts how many times each character appears and gives a percentage compared to the total. It also counts the number of words and the occurrence of a three letter sequence "ted".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <string>

using namespace std;

int charcount(char a[],char k,int size)
{
	int c=0;

	for(int i=0;i<size;i++)
	{
		if (a[i]==k)
			c++;
		

	}



	return c;

}

int tedcount(char a[],int size)
{
	int c=0;
	for (int i=0;i<size;i++)
	{
		if ((a[i]=='t'||a[i]=='T')&&(a[i+1]=='e'||a[i+1]=='E')&&(a[i+2]=='d'||a[i+2]=='D'))
		{
			c++;
		}

		


	}



	return c;

}

int wordcount(const char* ca)
{
   if (ca == NULL)
      return 0;

   bool inSpaces = true;
   int numWords = 0;

   while (*ca != NULL)
   {
      if (isspace(*ca))
      {
         inSpaces = true;
      }
      else if (inSpaces)
      {
         numWords++;
         inSpaces = false;
      }

      ++ca;
   }

   return numWords;
}


int main()
{
	string input;
	int lcount[96];
	float total=0,total2=0,maxtotal,tedtotal,wc,tc;
	for(int i=0;i<94;i++)
	{
		lcount[i]=0;

	}

	cout<<"please enter the characters to be counted"<<endl;
	getline(cin,input);


	char *t= new char[input.size()+1];
	int size=input.size();
	t[input.size()]=0;
	memcpy(t,input.c_str(),input.size());
	char carray[91]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','~','`','!','@','#','$','%','^','&','*','(',')','_','-','=','+','[','{','}',']','|','\\','/','<','>',',','.','?','/'};


	for(int i=0;i<size;i++)
	{

		lcount[i]=charcount(t,carray[i],size);

		
	}

	for (int i=0;i<62;i++)
	{
		total=lcount[i]+total;

}
	for (int i=63;i<91;i++)
	{
		total2=lcount[i]+total2;

}

	maxtotal=total+total2;

	for(int i=0;i<62;i++)
	{
		
		cout<<"Character:"<<carray[i]<<"  Count "<<lcount[i]<<endl;
		cout<<" total frequency %"<<(lcount[i]/maxtotal)*100<<endl;
		cout<<" alphanumeric only % "<<(lcount[i]/total)*100<<endl;
		cout<<"-----------------------------------------"<<endl;
		
	}

	for(int i=63;i<91;i++)
	{

		
	cout<<"Character:"<<carray[i]<<"  Count "<<lcount[i]<< " total frequency %"<<(lcount[i]/maxtotal)<<endl;
		

	}
	cout<<"------------------------------------"<<endl;

	wc=wordcount(t);
	tc=tedcount(t,size);
	tedtotal=(tc/wc)*100;
	cout<<"total number of characters:"<<maxtotal<<endl;
	cout<<"Number of words is  "<<wc<<endl;
	cout<<"Number of  words with ted is "<<tc<<endl;
	
	cout<<"frequency of words with ted is %"<<tedtotal<<endl;



	system("pause");
	return 0;
}



The word and ted count works. but the character counter breaks whenever I use upper-case,numbers,or other characters like ! or % because it doesn't count them for some reason. It's killing me because I know i'm missing something simple. Any hints would be greatly appreciated
You can't use the same counter i for two arrays lcount and carray, as the two have different sizes. For short inputs you get incorrect letter count, but for inputs over 91 characters your program is likely to crash.
1
2
3
4
for (int i = 0; i<size; i++)
{
   lcount[i] = charcount(t, carray[i], size);
}
Last edited on
I think I get what you're saying. Because I'm comparing the array to a key I made the for loop should also be set to the same size. Because the size wasn't the same it made it so my short test sentences didn't get far enough in the array to actually see the uppercase letters/numbers.
Topic archived. No new replies allowed.