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, 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.
Reputation Points: 3281
Solved Threads: 895
Posting Sage
Online 7,748 posts
since May 2006