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

Dani AI

Generated

A concise, practical fix and why the compiler complained

The simplest change is to have the counting function accept the character to look for; keep the declaration and definition in sync (that is what caused the "conflicting types" error saw). was on the right track adding a second parameter, and correctly pointed out the prototype must be updated. Use safe input (avoid gets) and prefer size_t for counts.

Example (safe, case-insensitive option, different from the snippets already posted):

#include <stdio.h>
#include <string.h>
#include <ctype.h>

size_t count_ch(const char *s, int target, int case_insensitive)
{
    size_t n = 0;
    if (case_insensitive) {
        int t = tolower(target);
        for (; *s; ++s)
            if (tolower((unsigned char)*s) == t) ++n;
    } else {
        for (; *s; ++s)
            if ((unsigned char)*s == (unsigned char)target) ++n;
    }
    return n;
}

int main(void)
{
    char buf[128];
    int ch;

    puts("Type a word or phrase:");
    if (!fgets(buf, sizeof buf, stdin)) return 1;
    buf[strcspn(buf, "\n")] = '\0';

    puts("Character to count (press key then Enter):");
    ch = getchar();
    printf("Occurrences: %zu\n", count_ch(buf, ch, 1));
    return 0;
}

Troubleshooting / tips

  • The "conflicting types" error means the forward declaration (prototype) doesn't match the later definition. Either update the prototype to include the extra parameter or move the full function above main. Mentioned earlier by .
  • gets is unsafe; fgets prevents buffer overflows.
  • For a full histogram of every byte value use an array sized by UCHAR_MAX+1 from <limits.h> and index with (unsigned char) to avoid negative indices (an improved version of 's idea).
  • If the input may be UTF-8, counting bytes is not the same as counting user-visible characters; use a proper Unicode-aware library when needed.

Recommended Answers

All 7 Replies

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 Member #46692

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.