C++ Programming Code Examples
C++ > Code Snippets Code Examples
Append two strings
/* Append two strings */
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "cat" );
string string3;
string3.assign( string1 );
cout << "string1: " << string1
<< "\nstring3: " << string3 << "\n\n";
string3 += "pet";
string3.append( string1, 1, string1.length() - 1 );
cout << "string1: " << string1
<< "\nstring3: " << string3 << "\n\n";
return 0;
}
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).
Append to string. This function is used to extend the string by appending at the end of the current value. Extends the string by appending additional characters at the end of its current value. Append is a special function in the string library of C++ which is used to append string of characters to another string and returns * this operator. This is similar to push_back or += operator, but it allows multiple characters to append at the same time. This means a character array can also be appended but it doesn't allow appending a single character. It also allows to append a specific part of the second string to the first string or defining the number of time a string must be appended. An iterator range is also provided to iterate over the character of strings.
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).
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
#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.
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.
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.
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.
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.
Linearly traverse the data array. Keep track of the "Smallest Number". Simultaneously keep updating the second smallest number also. A function to calculate second. If array element
Addition, Subtraction, Multiplation, Result to decimal. Reverse second binary operand and Creating an array for addition. Addition of all columns without carry. Final calculations and
To encrypt & decrypt file content in C++, you have to enter the file name with extension to encrypt & decrypt the content present inside the file. Now open that file using the function
"Built-in Functions" are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc.