944,125 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19668
  • C RSS
Apr 5th, 2007
0

Counting specific characters in a string

Expand 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. }
Last edited by ~s.o.s~; Apr 5th, 2007 at 3:47 pm. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psuspect is offline Offline
3 posts
since Apr 2007
Apr 5th, 2007
0

Re: Counting specific characters in a string

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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 5th, 2007
0

Re: Counting specific characters in a string

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psuspect is offline Offline
3 posts
since Apr 2007
Apr 5th, 2007
0

Re: Counting specific characters in a string

modify the function prototype to
  1. int countchar(const char[], char);
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Queatrix is offline Offline
13 posts
since Mar 2007
Apr 5th, 2007
0

Re: Counting specific characters in a string

Click to Expand / Collapse  Quote originally posted by Queatrix ...
modify the function prototype to
  1. int countchar(const char[], char);
That fixes one of the problems. But there are others.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 6th, 2007
0

Re: Counting specific characters in a string

Click to Expand / Collapse  Quote originally posted by psuspect ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Jun 27th, 2010
-4
Re: Counting specific characters in a string
#include<stdio.h>
#include<conio.h>
void main()
{
int i,count=0;
char ,str;
printf("enter the string");
scanf("%s",&str);
for(i=0;str='\0';i++)
{
if (str='a')
{
count++;
}
}
return count;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonisneha is offline Offline
1 posts
since Jun 2010
Aug 24th, 2010
0
Re: Counting specific characters in a string
#include <stdio.h>
#include <conio.h>
int countchar( char list[10]);
int main()
{
char message [10];
int numchar;
clrscr();
printf("\nType in a word: ");
gets(message);
numchar = countchar (message);
printf ("The number of characters entered is %d\n", numchar);
getch ();
return 0;
}

int countchar( char list[10])
{
int i=0, count=0 ;
for( ; list[i] != 0 ; ++i )
if( list[i] == 'a' )
++count ;
return count ;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bluntaxe1 is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Allocate memory for an array inside a struct
Next Thread in C Forum Timeline: Pointers program using Dynamic Memory Allocation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC