63.7k views
2 votes
Assign a pointer to any instance of searchChar in personName to searchResult.#include #include using namespace std;int main() {char personName[100];char searchChar;char* searchResult = nullptr;cin.getline(personName, 100);cin >> searchChar;/* Your solution goes here */if (searchResult != nullptr) {cout << "Character found." << endl;}else {cout << "Character not found." << endl;}return 0;}

1 Answer

5 votes

Answer:

Here it the solution statement:

searchResult = strchr(personName, searchChar);

This statement uses strchr function which is used to find the occurrence of a character (searchChar) in a string (personName). The result is assigned to searchResult.

Headerfile cstring is included in order to use this method.

Step-by-step explanation:

Here is the complete program

#include<iostream> //to use input output functions

#include <cstring> //to use strchr function

using namespace std; //to access objects like cin cout

int main() { // start of main() function body

char personName[100]; //char type array that holds person name

char searchChar; //stores character to be searched in personName

char* searchResult = nullptr; // pointer. This statement is same as searchResult = NULL

cin.getline(personName, 100); //reads the input string i.e. person name

cin >> searchChar; // reads the value of searchChar which is the character to be searched in personNa,e

/* Your solution goes here */

searchResult = strchr(personName, searchChar); //find the first occurrence of a searchChar in personName

if (searchResult != nullptr) //if searchResult is not equal to nullptr

{cout << "Character found." << endl;} //displays character found

else //if searchResult is equal to null

{cout << "Character not found." << endl;} // displays Character not found

return 0;}

For example the user enters the following string as personName

Albert Johnson

and user enters the searchChar as:

J

Then the strchr() searches the first occurrence of J in AlbertJohnson.

The above program gives the following output:

Character found.

Assign a pointer to any instance of searchChar in personName to searchResult.#include-example-1