#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.
That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
You do nothing! A sequence of numbers will contain no X and so nothing needs doing!
a promlem to solve an equation or a assigment
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.
That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
You do nothing! A sequence of numbers will contain no X and so nothing needs doing!
a promlem to solve an equation or a assigment
No.
unsigned find (const char* str, char c) { unsigned total; total = 0; if (str!=NULL) while (*str) { if (*str++==c) ++total; } return total; }
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.
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.
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.
This functionality is already in Java. String.toLowerCase() and String.toUpperCase() will take care of it for you.
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.
8086 assembly language program to check wether given number is perfect or not