Thread: looping and counting

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    30

    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?
    Last edited by phoebus; 04-29-2008 at 10:00 PM.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    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)
    Last edited by abh!shek; 04-29-2008 at 10:58 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.
    Last edited by robwhit; 04-30-2008 at 02:49 PM. Reason: bug fix

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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.
    Could you please detail?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.")
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Thanks, that settle the problem.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by robwhit View Post
    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?
    Last edited by abh!shek; 04-30-2008 at 11:07 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    ...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. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abk View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by root4 View Post
    Could you please detail?
    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. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by root4 View Post
    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. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I knew it had to do with interpreted versus compiled but yeah. I have some leaning to do about that.

Popular pages Recent additions subscribe to a feed