C++ Programming Code Examples
C++ > Strings Code Examples
Programming Code to Concatenate String
/* Programming Code to Concatenate String
To concatenate or to append one string to another string in C++ Programming, you have to ask to the user to enter the two string and start concatenating one string into other using the strcat() function as shown here in the following program.
Following C++ program ask the user to enter the two string to concatenate it using the function strcat() of string.h library. This function takes the two argument like strcat(str1, str2). Here string of str2 will be appended at the end of the string str1 :
C++ Program - Concatenate String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str1[50], str2[50];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
strcat(str1, str2);
cout<<"String after concatenation is "<<str1;
getch();
}
Concatenate strings. The strcat() function in C++ appends a copy of a string to the end of another string. It is defined in <cstring> header file. Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. destination and source shall not overlap. The strcat() function takes two arguments: destination and source. This function appends a copy of the character string pointed to by source to the end of string pointed to by destination. The null terminating character at the end of destination is replaced by the first character of source and the resulting character is also null terminated.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
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.
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).
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.
#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.
Get string from stdin. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str. Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows). On success, the function returns str.
In C++ coding, we take a 'For Loop' which will start from 1 up to number that has entered to check whether it is Prime Number or not with in for loop we set an 'IF' condition and placed
While Loop: While a given Expression is true it "Repeats the Statement" in the loop body. Before executing the Loop Body it tests the condition for True or False. Do...While Loop:
Algorithm finds the median of 2 sorted arrays using binary search approach. Takes the input of 'n' Data Elements of both the arrays. Using decrease, conquer method find the combined