C++ Programming Code Examples
C++ > Code Snippets Code Examples
String: find( ) and rfind( )
/* String: find( ) and rfind( ) */
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string stringObject1 = "Quick of Mind, Strong of Body, Pure of Heart";
string stringObject2;
i = stringObject1.find("Quick");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
i = stringObject1.find("Strong");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
i = stringObject1.find("Pure");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
// find list "of"
i = stringObject1.rfind("of");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
return 0;
}
Maximum value for size_t. This value, when used as the value for a len parameter in string's member functions, means until the end of the string. This constant is defined with a value of -1. Since size_t is an unsigned integral type, -1 is the largest possible representable value for this type. To put it simply, think of npos as no-position. As a return value, it is usually used to indicate that no matches were found in the string. Thus, if it returns true, matches were found at no positions (i.e., no matches).
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
Assign content to string. Assigns a new value to the string, replacing its current contents. This functions assigns a new value to the string, replacing all its current contents. assign() is a library function of "string" class and it is used to assign, replace the string. This function is overloaded function, we can use it for many purposes i.e. to assign the string, replace a part of the string, any constant value etc.
Find last occurrence of content in string. Searches the string for the last occurrence of the sequence specified by its arguments. The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it will return string::npos which denotes the pointer is at the end of the string. When pos is specified, the search only includes sequences of characters that begin at or before position pos, ignoring any possible match beginning after pos.
Find content in string. Searches the string for the first occurrence of the sequence specified by its arguments. When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos. Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match. Function returns the position of the first character of the first match. If no matches were found, the function returns string::npos.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Strings are objects that represent sequences of characters. The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters. The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types. Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
Return length of string. Returns the length of the string, in terms of bytes. This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity. Note that string objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8). Both string::size and string::length are synonyms and return the same value.
Program to "shuffle array" using Fisher-Yates algorithm. This algorithm for "generating" a 'Random Permutation' of a finite set-in plain terms, for randomly shuffling the set. Variant
C++ Program to construct an Expression tree for an "Infix Expression". A binary expression tree is a specific application of a "binary tree" to evaluate certain expressions. These trees
2 const variables row & col are used to define size. If we do not make both const then error found because without "const reserve word" they are behaving as variable. Before placing
Program to implement "Jarvis March" to find convex hull. The idea of Jarvis's Algorithm is simple, we start from the 'leftmost point' (or point with minimum x coordinate value) and
A program which takes some 'Elements' in an array and a 'Key' in variable then program use 'Binary Search' C++ Algorithm to find the key. One to sort array using 'Bubble Sort'. Second