# looping and counting

This is a discussion on looping and counting within the C Programming forums, part of the General Programming Boards category; I have a string. Now I want to count the frequency of each letter a->b, A->B appear in the string. ...

1. ## looping and counting

I have a string. Now I want to count the frequency of each letter a->b, A->B appear in the string. I try to do that like this:
Code:
```for(i=0;i<strlen(str);i++){
if      ( str[i] == 'a')	a++;
else if ( str[i] == 'b')	b++;
else if ( str[i] == 'c')	c++;
else if ( str[i] == 'd')	d++;
else if ( str[i] == 'e')	e++;
else if ( str[i] == 'f')	f++;
else if ( str[i] == 'g')	g++;
else if ( str[i] == 'h')	h++;
else if ( str[i] == 'i')	i++;
else if ( str[i] == 'j')	j++;
else if ( str[i] == 'k')	k++;
else if ( str[i] == 'l')	l++;
else if ( str[i] == 'm')	m++;
else if ( str[i] == 'n')	n++;
else if ( str[i] == 'o')	o++;
else if ( str[i] == 'p')	p++;
else if ( str[i] == 'q')	q++;
else if ( str[i] == 'r')	r++;
else if ( str[i] == 's')	s++;
else if ( str[i] == 't')	t++;
else if ( str[i] == 'u')	u++;
else if ( str[i] == 'v')	v++;
else if ( str[i] == 'w')	w++;
else if ( str[i] == 'x')	x++;
else if ( str[i] == 'y')	y++;
}```
My question is: Is there a shorter way doing that?

2. something (but not exactly) like

Code:
```int n[26]={0};
int lol;
for(i=o;i<strlen(str);i++)

{
lol=(int)str[i]-97;
n[lol]++;
}```
After executing this the frequency of a,b,c will be found in n[0], n[1], n[2] respectively.

But I'm not sure this may be completely wrong.(I just want to post here)

3. I think you meant i=0, not i=o.

You should use 'a' instead of 97, and that wouldn't work on all platforms because a-z are not guaranteed to be consecutive.

Basically, what you're looking for is reflection, which C and C++ don't have.

edit:

...or...
Code:
```char alpha[]="abcdefghijklmnopqrstuvwxyz";
int *address[] = { &a, &b, &c, &d, etc... &z };

for (int i=0; i < strlen(str); i++)
{
if (isalpha(str[i])
{
(*address[(strchr(alpha, i) - alpha)/sizeof(*alpha)]) ++ ;
}
}```
disclaimer: untested.

4. You should use 'a' instead of 97, and that wouldn't work on all platforms because a-z are not guaranteed to be consecutive.
Encoding constraints are specified in the C standard, and there are high chances it
requires an ASCII-compliant encoding (c99 upgrades to UTF, which is still compliant),
If this is the case, the order of 'a' to 'z' is ensured (and the others).

Basically, what you're looking for is reflection, which C and C++ don't have.

5. Encoding constraints are specified in the C standard, and there are high chances it
requires an ASCII-compliant encoding (c99 upgrades to UTF, which is still compliant),
If this is the case, the order of 'a' to 'z' is ensured (and the others).
C99 states in section 5.2.1:
"Two sets of characters and their associated collating sequences shall be defined: the set in which source files are written (the source character set), and the set interpreted in the execution environment (the execution character set). (...) The values of the members of the execution character set are implementation-defined."

However, although the relative order of 'a' to 'z' is not guaranteed, the relative order of '0' to '9' is guaranteed. ("In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.")

6. Thanks, that settle the problem.

7. Originally Posted by robwhit
I think you meant i=0, not i=o.
Yes.
You should use 'a' instead of 97, and that wouldn't work on all platforms because a-z are not guaranteed to be consecutive
I don't understand the C99 UTF coding standard upgrade compliant bla bla whatever.....but isn't there an ASCII table which everyone should follow ? I mean how come a-z are not guaranteed to be consecutive?

8. but isn't there an ASCII table which everyone should follow ?
Without referring to the ASCII table, tell me which character has the value of 33, and which character has the value of 64. If you took even a second for both of them combined, and got them right, you are too slow. Portability aside, 97 is a magic number that should be avoided.

9. ...but isn't there an ASCII table which everyone should follow?
ASCII is one encoding among several (adapted to each country).
What is said in the posts above is that the encoding chosen by one
compiler may not be the same than the encoding used by another
compiler (implementation-defined).
There are some constraints on the encoding (detailed in the
standard), but the ordering of letters as discussed in this
thread is not one of them.

10. Originally Posted by abk
I don't understand the C99 UTF coding standard upgrade compliant bla bla whatever.....but isn't there an ASCII table which everyone should follow ? I mean how come a-z are not guaranteed to be consecutive?
Because there are other character sets, albeit less common, that do not have a..z in consecutive order.
Example: http://en.wikipedia.org/wiki/EBCDIC

--
Mats

11. Originally Posted by root4
Reflection is a quality that allows you to match up source sode descriptions of objects with the actual objects. I think. Maybe somebody else would know more.

http://en.wikipedia.org/wiki/Reflection_(computer_science)

12. Reflection is a quality that allows you to match up source sode descriptions of objects with the actual objects.
Ok, we're speaking of the same thing. I'm not sure your definition is correct but nevermind.
The interesting question is how would you use this property to solve the problem discussed
in the thread? It's not obvious. I'm just curious, don't waste time on that

13. Originally Posted by root4
I'm not sure your definition is correct but nevermind.
It very well might be.

I thought that if you knew the name of the source variable you could use that with reflection to alter said variable by referring to it in a string. But I know C and C++ don't have it.

14. I thought that if you knew the name of the source variable you could use that with reflection to alter said variable by referring to it in a string.
This is indeed one way to implement the things, but you could imagine another way to define/access your objects' model as well as another way to modify them. If you're referring to Java for the
reflection implementation, except if there were some big changes, it is only limited to the
instrospection part (one side of the reflection property) and it can easily be achieved because
the code is interpreted (so the object names can be recorded and used later), this is much
less easy with some binary code (that's why C and C++ cannot do it [directly?]).

15. I knew it had to do with interpreted versus compiled but yeah. I have some leaning to do about that.

Page 1 of 2 12 Last