answersLogoWhite

0

#include

#include

size_t count_char (const std::string& s, const char c)

{

size_t count = 0;

size_t offset = 0;

while ((offset = s.find(c, offset)) != s.npos)

{

++count;

++offset;

}

return count;

}

int main()

{

std::string str {"Hello world!"};

std::cout << "String: "" << str << """ << std::endl;

for (auto c : str )

{

size_t count = count_char (str, c);

std::cout << "'" << c << "' occurs " << count << " time" << (count==1?"":"s") << "." << std::endl;

}

}

Example output:

String: "Hello world!"

'H' occurs 1 time.

'e' occurs 1 time.

'l' occurs 3 times.

'l' occurs 3 times.

'o' occurs 2 times.

' ' occurs 1 time.

'w' occurs 1 time.

'o' occurs 2 times.

'r' occurs 1 time.

'l' occurs 3 times.

'd' occurs 1 time.

'!' occurs 1 time.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Write a program that stores vowels in an array When you program is given a character it should indicate whether the character is vowel or not?

That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.


Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......


How can I efficiently remove all occurrences of C strings from a given text or data set?

To efficiently remove all occurrences of C strings from a given text or data set, you can use a programming language like Python or C to search for and replace the C strings with an empty string. This can be done using functions like replace() in Python or std::string::replace() in C.


Program to generate tokens from a given expression string?

a promlem to solve an equation or a assigment


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do you write a program that removes all occurrences of a given X in a given sequence of numbers?

You do nothing! A sequence of numbers will contain no X and so nothing needs doing!


Write a C program to find occurrences of given character in a sentences?

unsigned find (const char* str, char c) { unsigned total; total = 0; if (str!=NULL) while (*str) { if (*str++==c) ++total; } return total; }


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


What is th function of the charindex?

The CHARINDEX function in SQL is used to find the position of a specific character or substring within a string. It returns the starting position of the substring or character within the given string.


How do you delete the given string in java programming?

You usually do not need to delete a String, since when the program no longer refers to it, the garbage collector will clean it up.


How many 3s in 33333333333333333333333333333333333333333333333333333333333333333333333333300000000000000000000000000000000000000000000000000000888888888888888888888888888888888888888886666666666666666?

To find the number of times the digit '3' appears in the given string of numbers, we can count the occurrences. The digit '3' appears 64 times in the provided sequence.


Write a program to change the case of the given string?

This functionality is already in Java. String.toLowerCase() and String.toUpperCase() will take care of it for you.