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

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

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.

modify the function prototype to

int countchar(const char[], char);
Member Avatar for iamthwee

modify the function prototype to

int countchar(const char[], char);

That fixes one of the problems. But there are others.

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.

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

commented: Non-Standard, ugly, no code tags, missing a brace, unnecessary use of conio, wrong if-condition & str initialization. +0
commented: Plz plz don't dig out old posts...check the date for the thread u r posting to....and plz learn how to post code before posting !!! +0

#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 != 0 ; ++i )
if( list == 'a' )
++count ;
return count ;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.