C++ Programming Code Examples
C++ > Strings Code Examples
C++ Program to Implement Longest Prefix Matching
/* C++ Program to Implement Longest Prefix Matching
This C++ Program demonstrates the implementation of Longest Prefix Matching. */
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<stack>
using namespace std;
/* node Declaration */
struct node
{
char data;
node *child[128];
node()
{
for (int i = 0; i < 128; i++)
child[i] = NULL;
}
};
/* trie class Declaration */
class trie
{
private:
node *root;
public:
trie()
{
root = new_node(0);
}
node *new_node(int data)
{
node *Q = new node;
Q->data = data;
return Q;
}
void add(string S)
{
node *cur = root;
for (int i = 0; i < S.length(); i++)
{
if (!cur->child[S[i] - 'A'])
cur->child[S[i] - 'A'] = new_node(S[i]);
cur = cur->child[S[i] - 'A'];
}
}
void check(node *cur, string S, int i)
{
if (cur)
{
cout<<cur->data;
if (i < S.length())
check(cur->child[S[i] - 'A'], S, i + 1);
}
}
void checkroot(string S)
{
if (root && S.length() > 0 && S[0] > 'A')
check(root->child[S[0] - 'A'],S,1);
else
cout<<"\nEmpty root \n";
}
};
/* Main */
int main()
{
trie dict;
dict.add("are");
dict.add("area");
dict.add("base");
dict.add("cat");
dict.add("cater");
dict.add("basement");
string input;
input = "caterer";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
input = "basement";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
input = "are";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
input = "arex";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
input = "basemexz";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
input = "xyz";
cout<<input << ": ";
dict.checkroot(input);
cout<<endl;
return 0;
}
Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.
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.
In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
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).
#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.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
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.
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
Return length of string. Returns the length of the string, in terms of bytes. This function is used to find the length of the string in terms of bytes. This is the actual number of bytes that conform the contents of the string , which is not necessarily equal to the storage capacity. 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).
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
If there exists "Multiple Strongly Connected" component, graph is not strongly connected, it is otherwise. Implementation of Kosaraju's Algorithm to "Print all SCCs". Fills Stack with
Program to find the connected components of the "undirected graph". This can be done using depth first search. Implementation of "Kosaraju's algorithm" to print all SCCs. Fills
This Loop would never end as 'decrementing' the value of i which is 1 so the condition i<=6 would never return false. A "While Loop" that never stops is said to be "Infinite While Loop",