modify the function to
int countchar( const char list[], char what )
{
int i=0, count=0 ;
for( ; list[i] != 0 ; ++i )
if( list[i] == what ) ++count ;
return count ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
modify the function prototype to
int countchar(const char[], char);
That fixes one of the problems. But there are others.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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!
#include <stdio.h>
#define MAXNUM 15
int countchar (char []);
int main()
{
char message [MAXNUM];
int numchar;
printf("\nType in a word: ");
gets(message);
numchar = countchar (message);
printf ("The number of characters entered is %d\n", numchar);
return 0;
} // END OF MAIN
//FUNCTION IMPLEMENTATION
int countchar (char list[])
{
int i, count = 0;
for (i = 0; list[i] != '\0'; i++)
count++;
return (count);
}
From this point, changecount 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 changemessage and line to unsigned char to properly count the high-bit ASCII characters.
Also, see this .
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944