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.