Count the number occurrences of a character in a string

0 votes
What's the simplest way to count the number of occurrences of a character in a string?

e.g. count the number of times 'a' appears in 'Mary had a little lamb'
Dec 28, 2020 in Python by Roshni
• 10,520 points
14,057 views

3 answers to this question.

0 votes

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
answered Dec 28, 2020 by Gitika
• 65,890 points
0 votes

Another way could be:

Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string.

Examples:

Input : str = "endtoend"
         c = 'e'
Output : 2
'e' appears four times in str.

Input : str = "abccdefgaa"
          c = 'a' 
Output : 3
'a' appears three times in str.


// C++ program to count occurrences of a given

// character

#include <iostream>

#include <string>

using namespace std;

  

// Function that return count of the given 

// character in the string

int count(string s, char c)

{

    // Count variable

    int res = 0;

  

    for (int i=0;i<s.length();i++)

  

        // checking character in string

        if (s[i] == c)

            res++;

  

    return res;

}

  

// Driver code

int main()

{

    string str= "edureka";

    char c = 'e';

    cout << count(str, c) << endl;

    return 0;

}

Output:

2

answered Dec 28, 2020 by Nikita
0 votes

The string count() method returns the number of occurrences of a substring in the given string. In simple words, count() method searches the substring in the given string and returns how many times the substring is present in it.

answered Dec 28, 2020 by Carlos

Related Questions In Python

+1 vote
1 answer
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,539 views
0 votes
1 answer

How to count the number of elements in a list?

To count the number of elements of ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
5,221 views
0 votes
1 answer

What is the Python logic to count the number of capital letters in a file?

Hi, You can use this piece of code, ...READ MORE

answered Jun 26, 2020 in Python by Roshni
• 10,520 points
2,112 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,397 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,006 views
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
35,928 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP