C++ Programming Code Examples
C++ > Strings Code Examples
From a String Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* From a String Object
This program takes a string object from the user and calculates the number of vowels, consonants, digits and white-spaces. */
#include <iostream>
using namespace std;
int main()
{
string line;
int vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
cout << "Enter a line of string: ";
getline(cin, line);
for(int i = 0; i < line.length(); ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
cout << "Digits: " << digits << endl;
cout << "White spaces: " << spaces << endl;
return 0;
}
main() Function in C++
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.
Syntax for main() Function in C++
void main()
{
............
............
}
void
void is a keyword in C++ language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value.
main
main is a name of function which is predefined function in C++ library.
In place of void we can also use int return type of main() function, at that time main() return integer type value.
1) It cannot be used anywhere in the program
a) in particular, it cannot be called recursively
b) its address cannot be taken
2) It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace).
3) It cannot be defined as deleted or (since C++11) declared with C language linkage, constexpr (since C++11), consteval (since C++20), inline, or static.
4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.
5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program).
6) (since C++14) The return type of the main function cannot be deduced (auto main() {... is not allowed).
7) (since C++20) The main function cannot be a coroutine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* simple code example by main() function in C++ */
#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
return 0;
}
Strings in C++ Language
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).
Declaration for Strings in C++
char str[4] = "C++ Programming";
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};
String Functions in C++
• int compare(const string& str): It is used to compare two string objects.
• int length(): It is used to find the length of the string.
• void swap(string& str): It is used to swap the values of two string objects.
• string substr(int pos,int n): It creates a new string object of n characters.
• int size(): It returns the length of the string in terms of bytes.
• void resize(int n): It is used to resize the length of the string up to n characters.
• string& replace(int pos,int len,string& str): It replaces portion of the string that begins at character position pos and spans len characters.
• string& append(const string& str): It adds new characters at the end of another string object.
• char& at(int pos): It is used to access an individual character at specified position pos.
• int find(string& str,int pos,int n): It is used to find the string specified in the parameter.
• int find_first_of(string& str,int pos,int n): It is used to find the first occurrence of the specified sequence.
• int find_first_not_of(string& str,int pos,int n ): It is used to search the string for the first character that does not match with any of the characters specified in the string.
• int find_last_of(string& str,int pos,int n): It is used to search the string for the last character of specified sequence.
• int find_last_not_of(string& str,int pos): It searches for the last character that does not match with the specified sequence.
• string& insert(): It inserts a new character before the character indicated by the position pos.
• int max_size(): It finds the maximum length of the string.
• void push_back(char ch): It adds a new character ch at the end of the string.
• void pop_back(): It removes a last character of the string.
• string& assign(): It assigns new value to the string.
• int copy(string& str): It copies the contents of string into another.
• char& back(): It returns the reference of last character.
• Iterator begin(): It returns the reference of first character.
• int capacity(): It returns the allocated space for the string.
• const_iterator cbegin(): It points to the first element of the string.
• const_iterator cend(): It points to the last element of the string.
• void clear(): It removes all the elements from the string.
• const_reverse_iterator crbegin(): It points to the last character of the string.
• const_char* data(): It copies the characters of string into an array.
• bool empty(): It checks whether the string is empty or not.
• string& erase(): It removes the characters as specified.
• char& front(): It returns a reference of the first character.
• string& operator+=(): It appends a new character at the end of the string.
• string& operator=(): It assigns a new value to the string.
• char operator[](pos): It retrieves a character at specified position pos.
• int rfind(): It searches for the last occurrence of the string.
• iterator end(): It references the last character of the string.
• reverse_iterator rend(): It points to the first character of the string.
• void shrink_to_fit(): It reduces the capacity and makes it equal to the size of the string.
• char* c_str(): It returns pointer to an array that contains null terminated sequence of characters.
• const_reverse_iterator crend(): It references the first character of the string.
• reverse_iterator rbegin(): It reference the last character of the string.
• void reserve(inr len): It requests a change in capacity.
• allocator_type get_allocator();: It returns the allocated object associated with the string.
Non-member Function Overloads
• operator+
Concatenate strings (function )
• relational operators
Relational operators for string (function )
• swap
Exchanges the values of two strings (function )
• operator>>
Extract string from stream (function )
• operator<<
Insert string into stream (function )
• getline
Get line from stream into string (function )
Operators used for String Objects
• =: assignment
• +: concatenation
• ==: Equality
• !=: Inequality
• <: Less than
• <=: Less than or equal
• >: Greater than
• >=: Greater than or equal
• []: Subscription
• <<: Output
• >>: Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* C++ String Library */
/* The C-Style Character String */
// C++ Program to demonstrate the working of getline(), push_back() and pop_back()
#include <iostream>
#include <string> // for string class
using namespace std;
// Driver Code
int main()
{
// Declaring string
string str;
// Taking string input using getline()
getline(cin, str);
// Displaying string
cout << "The initial string is : ";
cout << str << endl;
// Inserting a character
str.push_back('s');
// Displaying string
cout << "The string after push_back operation is : ";
cout << str << endl;
// Deleting a character
str.pop_back();
// Displaying string
cout << "The string after pop_back operation is : ";
cout << str << endl;
return 0;
}
getline() Function in C++
Get line from stream into string. The cin is an object which is used to take input from the user but does not allow to take the input in multiple lines. To accept the multiple lines, we use the getline() function. It is a pre-defined function defined in a <string.h> header file used to accept a line or a string from the input stream until the delimiting character is encountered.
Syntax for getline() Function in C++
// (1)
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
// (2)
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
is
istream object from which characters are extracted.
str
string object where the extracted line is stored. The contents in the string before the call (if any) are discarded and replaced by the extracted line.
delim
It is the delimiting character.
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Note that any content in str before the call is replaced by the newly extracted sequence.
Each extracted character is appended to the string as if its member push_back was called.
Function returns the same as parameter is.
A call to this function may set any of the internal state flags of is if:
eofbit: The end of the source of characters is reached during its operations.
failbit: The input obtained could not be interpreted as a valid textual representation of an object of this type. In this case, distr preserves the parameters and internal data it had before the call. Notice that some eofbit cases will also set failbit.
badbit: An error other than the above happened.
(see ios_base::iostate for more info on these)
Additionally, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions, an exception of type ios_base::failure is thrown.
Complexity
Unspecified, but generally linear in the resulting length of str.
Iterator validity
Any iterators, pointers and references related to str may be invalidated.
Data races
Both objects, is and str, are modified.
Exception safety
Basic guarantee: if an exception is thrown, both is and str end up in a valid state.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* get line from stream into string by getline() function code example */
// C++ program to demonstrate anomaly of delimitation of getline() function
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int id;
// Taking id as input
cout << "Please enter your id: \n";
cin >> id;
// Takes the empty character as input
cout << "Please enter your name: \n";
getline(cin, name);
// Prints id
cout << "Your id : " << id << "\n";
// Prints nothing in name field
// as "\n" is considered a valid string
cout << "Hello, " << name
<< " welcome to world !\n";
// Again Taking string as input
getline(cin, name);
// This actually prints the name
cout << "Hello, " << name
<< " welcome to world !\n";
return 0;
}
Namespaces in C++ Language
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 namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
namespace namespace_name {
// code declarations
}
name::code; // code could be variable or function.
Using Directive
You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace.
Discontiguous Namespaces
A namespace can be defined in several parts and so a namespace is made up of the sum of its separately defined parts. The separate parts of a namespace can be spread over multiple files.
So, if one part of the namespace requires a name defined in another file, that name must still be declared. Writing a following namespace definition either defines a new namespace or adds new elements to an existing one:
namespace namespace_name {
// code declarations
}
Nested Namespaces
Namespaces can be nested where you can define one namespace inside another name space as follows:
namespace namespace_name1 {
// code declarations
namespace namespace_name2 {
// code declarations
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* namespaces in C++ language */
// A C++ code to demonstrate that we can define
// methods outside namespace.
#include <iostream>
using namespace std;
// Creating a namespace
namespace ns
{
void display();
class happy
{
public:
void display();
};
}
// Defining methods of namespace
void ns::happy::display()
{
cout << "ns::happy::display()\n";
}
void ns::display()
{
cout << "ns::display()\n";
}
// Driver code
int main()
{
ns::happy obj;
ns::display();
obj.display();
return 0;
}
#include Directive in C++
#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.
Syntax for #include Directive in C++
#include "user-defined_file"
#include <header_file>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* using #include directive in C language */
#include <stdio.h>
int main()
{
/*
* C standard library printf function
* defined in the stdio.h header file
*/
printf("I love you Clementine");
printf("I love you so much");
printf("HappyCodings");
return 0;
}
Logical Operators in C++
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:
&&
Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
The logical AND operator && returns
true - if and only if all the operands are true.
false - if one or more operands are false.
||
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
The logical OR operator || returns
true - if one or more of the operands are true.
false - if and only if all the operands are false.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(A && B) is true.
The logical NOT operator ! is a unary operator i.e. it takes only one operand.
It returns true when the operand is false, and false when the operand is true.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.
The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds to the Boolean logical operation AND, which yields true if both its operands are true, and false otherwise. */
#include <iostream>
using namespace std;
main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout << "Line 1 - Condition is true"<< endl ;
}
if(a || b) {
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if(a && b) {
cout << "Line 3 - Condition is true"<< endl ;
} else {
cout << "Line 4 - Condition is not true"<< endl ;
}
if(!(a && b)) {
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}
For Loop Statement in C++
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.
Syntax of For Loop Statement in C++
for (initialization; condition; update) {
// body of-loop
}
initialization
initializes variables and is executed only once.
condition
if true, the body of for loop is executed, if false, the for loop is terminated.
update
updates the value of initialized variables and again checks the condition.
A new range-based for loop was introduced to work with collections such as arrays and vectors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* For Loop Statement in C++ Language */
// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}
If Else Statement in C++
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,
Syntax for If Statement in C++
if (condition) {
// body of if statement
}
Syntax for If...Else Statement
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
Syntax for If...Else...Else If Statement in C++
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
Syntax for If Else If Ladder in C++
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* If Else Statement in C++ Language */
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
int a = 100;
// check the boolean condition
if( a < 20 ) {
// if condition is true then print the following
cout << "a is less than 20;" << endl;
} else {
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}
If Else If Ladder in C/C++
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.
Syntax of if...else Ladder in C++
if (Condition1)
{ Statement1; }
else if(Condition2)
{ Statement2; }
.
.
.
else if(ConditionN)
{ StatementN; }
else
{ Default_Statement; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* write a C program which demonstrate use of if-else-if ladder statement */
/* Program to Print Day Names using Else If Ladder in C++*/
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter Day Number: ";
cin >> day;
cout << "Day is ";
if (day == 1)
cout << "Sunday" << endl;
else if (day == 2)
cout << "Monday" << endl;
else if (day == 3)
cout << "Tuesday" << endl;
else if (day == 4)
cout << "Wednesday" << endl;
else if (day == 5)
cout << "Thursday" << endl;
else if (day == 6)
cout << "Friday" << endl;
else
cout << "Saturday" << endl;
return 0;
}
length() Function in C++
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).
Syntax for length() Function in C++
size_t length() const noexcept;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* return length of string by length() function code example */
#include<iostream>
#include<cstring>
using namespace std;
main() {
string myStr = "This is a sample string";
char myStrChar[] = "This is a sample string";
cout << "String length using string::length() function: " << myStr.length() <<endl;
cout << "String length using string::size() function: " << myStr.size() <<endl;
cout << "String length using strlen() function for c like string: " << strlen(myStrChar) <<endl;
cout << "String length using while loop: ";
char *ch = myStrChar;
int count = 0;
while(*ch != '\0'){
count++;
ch++;
}
cout << count << endl;
cout << "String length using for loop: ";
count = 0;
for(int i = 0; myStrChar[i] != '\0'; i++){
count++;
}
cout << count;
}
String Relational Operators in C++
Relational operators for string. Performs the appropriate comparison operation between the string objects lhs and rhs.
The functions use string::compare for the comparison.
These operators are overloaded in header <string>.
If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
Syntax for String Relational Operators in C++
#include <string>
//(1) == : Equal to
bool operator== (const string& lhs, const string& rhs) noexcept;
bool operator== (const char* lhs, const string& rhs);
bool operator== (const string& lhs, const char* rhs);
//(2) != : Not equal to
bool operator!= (const string& lhs, const string& rhs) noexcept;
bool operator!= (const char* lhs, const string& rhs);
bool operator!= (const string& lhs, const char* rhs);
//(3) < : Less than
bool operator< (const string& lhs, const string& rhs) noexcept;
bool operator< (const char* lhs, const string& rhs);
bool operator< (const string& lhs, const char* rhs);
//(4) <= : Less than and equal to
bool operator<= (const string& lhs, const string& rhs) noexcept;
bool operator<= (const char* lhs, const string& rhs);
bool operator<= (const string& lhs, const char* rhs);
//(5) > : Greater than
bool operator> (const string& lhs, const string& rhs) noexcept;
bool operator> (const char* lhs, const string& rhs);
bool operator> (const string& lhs, const char* rhs);
//(6) >= : Greater than and equal to
bool operator>= (const string& lhs, const string& rhs) noexcept;
bool operator>= (const char* lhs, const string& rhs);
bool operator>= (const string& lhs, const char* rhs);
lhs, rhs
Arguments to the left- and right-hand side of the operator, respectively. If of type char*, it shall point to a null-terminated character sequence.
• lhs < rhs : A string lhs is smaller than rhs string, if either, length of lhs is shorter than rhs or first mismatched character is smaller.
• lhs > rhs : A string lhs is greater than rhs string, if either, length of lhs is longer than rhs or first mismatched character is larger.
• <= and >= have almost same implementation with additional feature of being equal as well.
• If after comparing lexicographically, both strings are found same, then they are said to be equal.
• If any of the points from 1 to 3 follows up then, strings are said to be unequal.
Function returns true if the condition holds, and false otherwise.
Complexity
Unspecified, but generally up to linear in both lhs and rhs's lengths.
Iterator validity
No changes
Data races
Both objects, lhs and rhs, are accessed.
Exception safety
If an argument of type char* does not point to null-terminated character sequence, it causes undefined behavior.
For operations between string objects, exceptions are never thrown (no-throw guarantee).
For other cases, if an exception is thrown, there are no changes in the string (strong guarantee).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered. */
// CPP code example to implement relational operators on String objects
#include<iostream>
using namespace std;
void relational_operation(string s1, string s2)
{
string s3 = s1 + s2;
if(s1 != s2)
cout << s1 << " is not equal to " << s2 << endl;
if(s1 > s2)
cout << s1 << " is greater than " << s2 << endl;
else if(s1 < s2)
cout << s1 << " is smaller than " << s2 << endl;
if(s3 == s1 + s2)
cout << s3 << " is equal to " << s1 + s2 << endl;
}
// Main function
int main()
{
string s1("Happy");
string s2("Happy 8) Codings");
relational_operation(s1, s2);
return 0;
}
This is a C++ Program to find the "cliques" of size k in a a graph. An "undirected graph" is formed by a finite set of vertices and a set of unordered pairs of vertices, which are called
First ask to enter the array size then it will ask to enter the array elements, then it will finally ask to enter a number to be search in array to check whether it is present in the array or not