Counting specific characters in a string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 3
Reputation: psuspect is an unknown quantity at this point 
Solved Threads: 0
psuspect psuspect is offline Offline
Newbie Poster

Counting specific characters in a string

 
0
  #1
Apr 5th, 2007
I have this program that counts characters in a string is there a way to modify this code so that for example it counts specific characters entered. Exmaple.. user entered Hello, and I wanted the program to say there are 2 "L"''s entered in that word. THanks for your help!

  1. #include <stdio.h>
  2. #define MAXNUM 15
  3.  
  4. int countchar (char []);
  5. int main()
  6.  
  7. {
  8. char message [MAXNUM];
  9. int numchar;
  10.  
  11. printf("\nType in a word: ");
  12. gets(message);
  13. numchar = countchar (message);
  14. printf ("The number of characters entered is %d\n", numchar);
  15. return 0;
  16.  
  17. } // END OF MAIN
  18.  
  19. //FUNCTION IMPLEMENTATION
  20.  
  21. int countchar (char list[])
  22. {
  23. int i, count = 0;
  24.  
  25. for (i = 0; list[i] != '\0'; i++)
  26. count++;
  27.  
  28. return (count);
  29. }
Last edited by ~s.o.s~; Apr 5th, 2007 at 3:47 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Counting specific characters in a string

 
0
  #2
Apr 5th, 2007
modify the function to
  1. int countchar( const char list[], char what )
  2. {
  3. int i=0, count=0 ;
  4. for( ; list[i] != 0 ; ++i )
  5. if( list[i] == what ) ++count ;
  6. return count ;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: psuspect is an unknown quantity at this point 
Solved Threads: 0
psuspect psuspect is offline Offline
Newbie Poster

Re: Counting specific characters in a string

 
0
  #3
Apr 5th, 2007
Originally Posted by vijayan121 View Post
modify the function to
  1. int countchar( const char list[], char what )
  2. {
  3. int i=0, count=0 ;
  4. for( ; list[i] != 0 ; ++i )
  5. if( list[i] == what ) ++count ;
  6. return count ;
  7. }


c:28: error: conflicting types for 'countchar'
c:4: error: previous declaration of 'countchar' was here

This is the error I get.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 13
Reputation: Queatrix is an unknown quantity at this point 
Solved Threads: 1
Queatrix Queatrix is offline Offline
Newbie Poster

Re: Counting specific characters in a string

 
0
  #4
Apr 5th, 2007
modify the function prototype to
  1. int countchar(const char[], char);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting specific characters in a string

 
0
  #5
Apr 5th, 2007
Originally Posted by Queatrix View Post
modify the function prototype to
  1. int countchar(const char[], char);
That fixes one of the problems. But there are others.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: Counting specific characters in a string

 
0
  #6
Apr 6th, 2007
Originally Posted by psuspect View Post
I have this program that counts characters in a string is there a way to modify this code so that for example it counts specific characters entered. Exmaple.. user entered Hello, and I wanted the program to say there are 2 "L"''s entered in that word. THanks for your help!

  1. #include <stdio.h>
  2. #define MAXNUM 15
  3.  
  4. int countchar (char []);
  5. int main()
  6.  
  7. {
  8. char message [MAXNUM];
  9. int numchar;
  10.  
  11. printf("\nType in a word: ");
  12. gets(message);
  13. numchar = countchar (message);
  14. printf ("The number of characters entered is %d\n", numchar);
  15. return 0;
  16.  
  17. } // END OF MAIN
  18.  
  19. //FUNCTION IMPLEMENTATION
  20.  
  21. int countchar (char list[])
  22. {
  23. int i, count = 0;
  24.  
  25. for (i = 0; list[i] != '\0'; i++)
  26. count++;
  27.  
  28. return (count);
  29. }
From this point, change count to an array of 256 values. Then for each character increment count using list[i] as the subscript:
count[list[i]]++;
This will count each and every character in your input, letters, numbers, SPACES, TABS, everything! Although you will have to change message and line to unsigned char to properly count the high-bit ASCII characters.

Also, see this.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5700 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC