954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Counting specific characters in a string

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);
}
psuspect
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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 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 ;
}

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

This is the error I get.

psuspect
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

modify the function prototype to

int countchar(const char[], char);
Queatrix
Newbie Poster
13 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

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
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

#include
#include
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;

sonisneha
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

#include
#include
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 ;
}

bluntaxe1
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You