C++

C++ Find the Word in a Char Array

We’ll discover how to use the character array in C++ in this article. We’ll explore how to declare, initialize, and get them in a variety of input/output functions. The term “string” represents a set of characters or letters. Strings that have been elements of the string class and C-style Strings are the 2 kinds of strings generally used in the C++ programming language.

C++ Program to Read a Character Array that has been Provided by the User

In this step, we execute a C++ program to show how to read the words of the character array entered by the user.

#include <iostream>
using namespace std;
int main()
{
    char s[150];

    cout <> s;
    cout << “You entered:<< s << endl;

    cout <> s;
    cout << “You entered:<<s<<endl;

    return 0;
}

Here, we introduce the header file <iostream> for input and output operations. Then, we have to utilize the standard namespace. Now it’s time to declare the main() function. Inside the body of the main() function, we initialize a character type string.

Now, we ask the user to input the string by the use of the ‘cout’ statement. Meanwhile ‘cin’ is used to get the string. Again, we have used the ‘cout’ statement so the user inputs the second string. To acquire this string, ‘cin’ is also applied. Then to end the program, we employ the return 0 statement.

In the second scenario, rather than showing the complete “information technology,” just “information” is printed. It’s because a space ” ” is considered a terminal element by the extraction operator ‘>>’.

Get and Present the Entire Line of the Character Array in C++

The cin.get() method could be used to retain information with white space. There are two parameters to this method. The first parameter would be the string’s title (the location of the first character of the string), and the second parameter would be the array’s maximum limit of size.

#include <iostream>
using namespace std;
int main()
{
    char s[150];

    cout << "Enter any string: ";
    cin.get(s, 150);

    cout << "You have entered: " << s << endl;
    return 0;
}

We are going to start the program by integrating the header file <iostream>. Meanwhile, we have been utilizing a standard namespace. In the next step, we declare a variable to store the character data type array within the body of the main() function. The character array size is specified here. 150 is the size of the entered array. Then ‘cout’ is applied to print the line ‘Enter any string’. The function cin.get() is utilized to get the string added by the user.

Here, we pass the string and its size as a parameter to the cin.get() function. Once again, the ‘cout’ statement is used to show the line ‘You have entered’. We get the string that the user has entered. To terminate the program, we employ the return 0 command.

Use a String to Call a Function

In the same manner that arrays are provided to a method, strings are given to it. There are two functions in this case. The display() is a function that displays a string to the characters. The argument is the main distinction between the 2 methods. The first display() method accepts an argument of a character array, whereas the second accepts an argument of string. This is related to function overloading.

In programming languages, we can specify multiple functions with identical names in the identical context.  Overloaded functions give a function varied semantics based on the categories and quantity of parameters.

#include <iostream>
using namespace std;
void display(char *);
void display(string);
int main()
{
    string s1;
    char s[150];
    cout << “Enter any string:;
    getline(cin, s1);
    cout << “Enter another string:;
    cin.get(s, 150, ‘\n’);
    display(s1);
    display(s);
    return 0;
}
void display(char str[])
{
    cout << “Entered char array is:<< str << endl;
}
void display(string str)
{
    cout << “Entered string is:<< str << endl;
}

Here initially, we include <iostream>. This header file handles the input and output functions. After this, we utilize the standard namespace. We utilize the void display() function. For the first display() method, we provide the constructor of the character data type. And for the second display() function, we pass a string as an argument of this function. The main() function is being called. In addition to this, we create a variable ‘s1’ for storing the string. In the next line, another variable ‘s’ is declared for storing the character.

Here, we set the array size. Further, we apply the ‘cout’ statement so the user enters any string of his own choice. Meanwhile, we utilize the getline() method and here we pass the entered string as a parameter of this function. Once again, we use the ‘cout’ statement to get the second array from the user. For this character type string, we employ the cin.get() function. This function holds two parameters: the entered character type string and its size.

Then, we apply the display() function to represent the values of these two strings. We use the return 0 command. Next, we call the void display() method. Here, we create a character string and then this character string is passed as an argument of the void display() method. After that, we use the ‘cout’ statement to obtain the character array. Then we utilized the void display() method and now the string is given as a parameter to this function. In this way, we find the string or character string.

Conclusion

In this article, we have examined three different techniques that are used to find the word in the character array. First, we ask the user to get the desired array of character data types then we apply numerous functions to get the entered word of the character data type. For this purpose, we have also utilized overloaded functions.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content