Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Code Snippets Code Examples

Using strpbrk

/* Using strpbrk */ #include <iostream> #include <string.h> using namespace std; int main() { char *string1 = "This is a test"; char *string2 = "beware"; cout << "Of the characters in \"" << string2 << "\"\n'" << *strpbrk( string1, string2 ) << '\'' << " is the first character to appear in\n\"" << string1 << '\"' << endl; return 0; }

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

Locate characters in string. Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches. The strpbrk() function in C++ searches for a set of characters present in a string in another string. The search does not include the terminating null-characters of either strings, but ends there. The strpbrk() function takes two null terminated byte string: str1 and str2 as its arguments. It searches the null terminated byte string pointed to by str1 for any character that is present in the string pointed to by str2 and returns the pointer to that character in str1.

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment

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.

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

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.




Arithmetic operators: +, -, *, /, %, addition, subtraction, multiplication, division, modulo. Assignments operators in C++ are: =, +=, -=, *=, /=, %=. Auto increment and decrement


To perform 'addition', 'subtraction', 'division' and 'multiplication' of any 2 numbers in C++, enter the 2 number and enter the operator to perform the particular math operation (+, -, *




The "expressions" are evaluated in order; if an expressionis true, the 'statement' or 'block of statement' associated with it is executed, and this terminates the whole chain. The Code for

Implement 'Binary search' using interpolation approach. The Time complexity is "O(log(n))". Implement the Binary Search on sorted array. Then Calculate mid value using 'interpolation'