Write a program to search how many times a given character is present in a given string in c

20 Sep 2022 Balmiki Mandal 0 C Programming

Count Occurrences of a Character in a String - C Program

"Search how many times a given character is present in a given string" refers to the process of counting the number of occurrences of a specific character in a string. For example, if we have the string "hello world" and we want to search for the character "l", we would count the number of times "l" appears in the string (which is two). This process can be useful in various programming tasks, such as data analysis, text processing, and pattern matching.

c program to search how many times a given character is present in a given string

#include<stdio.h>
void main()
{
    char ch,s[10];
    int i,c=0;
    printf("Enter the string:");
    scanf("%s",s);
    printf("Enter the character:");
    scanf(" %c",&ch);
    for(i=0;s[i];i++)
    {
        if(s[i]==ch)
        c++;
    }
    printf("%c is present %d Times\n",ch,c);
}

output:

Enter the string: ELECTRO4U
Enter the character:E
E is present 2 Times

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

Design a function to find string length in c

Design a Function to print the string character by character

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.