Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
C program to count the occurrences of a character in a string
Ans:
In this tutorial, you will learn writing program to
count occurrences of a character in a string.
The Complete logic behind to count character in a string is given below :
- In the first place, our program should take a string.
- And also a character which will be given by the user for which we want to count the occurrence in string.
- Then our program will count the total number of occurrences of that particular character in a string.
- For example, suppose some user has given a string ” quescol is a best educational website” and want to count the occurrence of the character ‘a’ in the given above string.
- Then our program will give output 3. Because ‘a’ is repeated 3 times in that string.
How our program will behave?
Our program will take a string as an input. A string could be with space or without space.
And also user have to give a character as an input for which they want to count the occurrence.
And in output, our program will give the total count of that particular character in the string.
For example:
Input: “Quescol provides previous year questions and answers” as string
and ‘a’ as a character
Output: 3
Method to count the occurrence of given character in a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int countOccurrence(char * str, char ch){
int i, count=0;
int len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] == ch)
{
count++;
}
}
return count;
}
void main(){
char str[100],ch,c;
int count;
printf("enter the string : \n");
scanf("%[^\n]%c",str,&c);
printf("enter a char want to count occurence : ");
scanf("%c",&ch);
count = countOccurrence(str,ch);
printf("total occurence of '%c': %d",ch,count);
getch();
}
Output:
Explanation of the above program
- Above program is for how to count occurrence of a given character in a string in C.
- In main() method, we have taken three char variable where str is an array of character to store string and a variable ch is to take a one character.
- All the logic of counting the occurrence of a character in a string is written in the method countOccurrence() method.
- Method countOccurrence() takes address of char array and a character as a parameter.
- In countOccurrence() method there are three int variable i, count and len. count variable is responsible to count the number of times a character in string.
- str variable of countOccurrence() method contains string and ch variable has character.
- Using for loop str is iterated each time and check if character of string is matching with the character which we want to count.
- If it matches then increase count by one otherwise do nothing just increase for loop times.
- And at last when for loop is iterated for the the length of string then loop condition will fail and method will return total count of character.
This was the all logic behind to count the number occurrence of given character in string using C.
I hope it has cleared your concept.
Also Prepare C interview programs given below
C Questions on number
- Write a program to reverse an integer in C.
- Write a program in C to check whether an integer is Armstrong number or not.
- Write a program in C to print the fibonacci series using recursive method.
- Write a program in C to check whether a number is palindrome or not using recursive method.
- Write a program in C to add two integer without using arithmetic + operator.
C Questions on String
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program