Thread: frequency character counter

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    frequency character counter

    Code:
    #include <stdio.h> 
    #include <string.h>
        
      int main(){  
         char message [50];
    	 int A=0,x=0,j;
    	 int counter[26];
    	 memset(counter,0,26);
    	 //memset(j,0,26);
    
    	 	
    	  printf("Enter your secret message:");
    	  gets(message);
    
    		  for( j=0; j<sizeof(message); j++){
    			  for(x=0;x<26;x++)
                 	if (message[j] == 65+x || message[j] == 65+x+32){
    					  printf("TeDeNg\n");
    					  counter[x]++;
    					  x=27;
    				  }
    		  }
    
    		//for(j=0;j<26;j++)
    		//	printf("%c or %c  total %d\n",65+j,65+j+32,counter[j]);
    
    		for(x=0;x<26;x++)
    			printf("%c or %c  total %d\n",65+x,65+x+32,counter[x]);
    
    
    
    			
         return 0;  
        }
    ++++++++++++++++++++++++++++++++++++++++++++++++=

    hi all.this program should count the frequency of character occured.
    the problem is it only counts frequency of char a to f..
    and start from g until z, it ill give strange char, like the variable hold null value..
    could anyone pls help me with this..

    thank u very much

    -hasoon-

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    	 memset(counter,0,26);
    Code:
    memset(counter,0,26*sizeof(int));
    Code:
    for( j=0; j<sizeof(message); j++){
    Code:
    for( j=0; j<strlen(message); j++){
    And there is an easier way to increment the counter. No (inner) loop is needed. I will leave it to you as an exercise.

    [edit]
    Oh and, avoid magic numbers. They make your code unreadable and unportable (some computers don't use ASCII).

    Code:
    65+x+32
    Code:
    'A' + x + ' '
    [/edit]

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Confusion in code

    Hello,
    Code:
      if (message[j] == 65+x || message[j] == 65+x+32)
              {
    	Printf("TeDeNg\n");
    	counter[x]++;
                    x=27;
              }
    Why do you need to assign x=27 and declaration of A there>

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Presumably to break out of the loop.

    Code:
    break;
    is probably better, though.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    thank you very much of ur help dear cyberfish ...i will try it out and do my exercise

    the declarartion of A is a mistake, which i make it as try ealier and i didnt delete.=)

    and the x=27 is for termination value.
    but actually i try to comment it and the code works fine.

    i will study more about that ..
    thanks for all response that i received.

    i like this forum.hehe

    -hasanah-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. about program that counts a character frequency
    By Unregistered in forum C++ Programming
    Replies: 15
    Last Post: 04-23-2002, 01:21 PM