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

10y 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.


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!


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.


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


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 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.


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.


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.


How do you write a c program for to replace a character of string either fro mbeginning or ending or at specified location?

The standard C library includes two simple utilities to find the first or last occurance of a given character within a given string, strchr() to search from the start and strrchr() for the reverse start from the end. Subject to the chosen search direction, you could use one of these two simple API. Both return a pointer to the location of the matching character within the string, or NULL if no such character is found. Note that this approach assumes a mutable string, a string stored in writeable memory. A string literal is a constant string and not generally mutable (even though some compilers are very casual about this). That is, strchr("the quick brown fox", 'q') will return a pointer to the first 'q', but since the string is a string of constant characters, you shouldn't use the pointer to change the letter found. To search and modify, you'd use string of variable characters, such as one allocated with the malloc() or strdup() standard API, or one created as a char array.