
It takes both arrays and size of the arrays as an argument.
Now, call one function to print the content of both arrays. Also, take the inputs for the second array secondArray and store it as we have done for the first one. Similarly, ask the user to enter the size of the second array and store it in size2 variable. The for loop will run for size1 times and each time it will read the array element from the user input. Now, using one for loop, ask the user to enter the values for the first array one by one. Read the user input value and store it in the size1 variable. This value should be less than or equal to MAXSIZE_ because we can’t take inputs exceeding this size. Ask the user to enter the size of the first array. i is used in the for loop and size1,size2 is the size of the first and the second array. We have also defined a few other integer variables to use in the program. You can see that size of both arrays is MAXSIZE_ or 100. First, create two integer arrays firstArray and secondArray. we are assuming that the maximum size of the array we will use in the program is 100. MAXSIZE_ is the maximum size of the array. The commented numbers in the above program denote the step numbers below : Then, we will continue this by comparing each character in the string with the null character.# include //1 # define MAX_SIZE 100 //8 void printArrays ( int *array1, int size1, int *array2, int size2 ) Explanation : We start from the beginning i.e., s which is equal to ‘H’ and compare it with null character. Printf(“The length of the string str is: %d\n”, length2) Printf(“The length of the string str is: %d\n”, length1) Loop from beginning till we reach null character. string a = “HELLO” Ĭhar str = “HELLO” // Declaration and Initialization Either we can use cout or we can convert the string data type into the C-style string using c_str() function and then use printf(). Similarly you cannot print the string data type with the printf() directly. It will take input until we press a space or enter. To take string data type as an input you cannot use scanf(). NOTE: All the above C-style string functions are in header file of C++. Length: Finding the length of the string.Ĭoncatenate: Adding one string at the end of the other.Ĭompare: Comparing two string if they are equal or not. An important thing to note is that all the functions which are used in C-style string function can also be applied on C++ string.
In C there are some predefined functions that can be used to do common operations on strings like copy, concatenation, comparison and finding the length. Let us now consider some functions which we can perform on strings.
For example in the above example str is the character ‘L’ and one can iterate over the string just like a normal array. Please note that even though C++ strings are a separate class type, their characters can still be referenced using a 0 based indexing, just like C strings. Strings can be declared and initialized as follows C++ provides an internal class type which is used for working with strings. However in C++ strings, one need not bother about the dimensions of the array, or the null character. In C, strings are just character arrays which end with a null character. It is different than your usual C-style string function in many ways. Now let us talk about C++ string data type. Similarly you can print the C-style string with the printf() with ’%s’ format specifier or cout. To take C-style string as an input you can simple use scanf() with ‘%s’ format specifier or we can also use cin. * Run a loop from starting index to ending index. Take a variable of integer type to store sum and initialize it to 0. Printf(“The third element of Arr is\n ”, Arr) Accessing and printing the 3rd element of the first array. To store the above elements in the array: dataType nameOfTheArray = It is necessary to define the size array at compile time. Here Arr is the name of array and 6 is the size. The way to do it is: dataType nameOfTheArray We can store it in an array of integer data type. Let us try to demonstrate this with an example: Let us say we have to store integers 2, 3, 5, 4, 6, 7. Each element can be individually referenced with its respective index.ġ-Dimensional array: It is a linear array that stores elements in a sequential order. It stores data elements in a continuous memory location. An array is a sequential collection of variables of same data type which can be accessed using an integer as index, that generally starts from 0.