80.5k views
4 votes
Assign a pointer to any instance of searchChar in personName to searchResult #include iostream» 2 #include 3 using namespace std; 4 5 int main() 6 har personName [190]"Albert Johnson" 7 char searchChar 1 test passed All tests passed char* searchResult nulipt r; 10 searchChar-J'; IYour solution goes here 12 13 if (sea rc h Result coutくく"character ! nullptr) { found." くく endl; 15 16 else 17 18 19 20 return 0; 21 cout << "Character not found." << endl; Run

2 Answers

5 votes

Answer:

deez lOL LOLOLOLOL

Step-by-step explanation:

User Sanjeet Kumar
by
4.1k points
3 votes

Answer:

Following are the code to the given question:

#include <iostream>//header file

#include <cstring>//header file

using namespace std;

int main() //main method

{

char personName[100] = "Albert Johnson";//defining a char array

char searchChar;//defining char variable

char *searchResult = nullptr;//use pointer type char variable to holding value as nullptr

searchChar = 'J';//holding char value

char *ptr = personName;//using pointer type char variable that holds a value

while (*ptr) //defining while loop that checks *ptr value

{

if (*ptr == searchChar) //use if that check *ptr value is equal to searchChar

{

searchResult = ptr;//holding ptr value in searchResult

}

ptr++;//incrementing ptr value

}

if (searchResult != nullptr) //use if that checks searchResult value not equal to nullptr

{

cout << "Character found." << endl;//print message

}

else //else block

{

cout << "Character not found." << endl;//print message

}

return 0;

}

Output:

Character found.

Step-by-step explanation:

In this code inside the main method, a char array is declared that holds values.

In the next step, some char variable and pointer type char variable are declared that holds values.

Inside the while loop, the pointer variable is used, and in the if the block it checks its value and holds its values.

Outside the loop, it checks searchResult value not equal to nullptr and prints the value as per the given condition.

User Tilde
by
4.5k points