Assign a pointer to any instance of searchChar in personName to searchResult.

  • Context: MHB 
  • Thread starter Thread starter carl123
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on using the C++ function strchr() to find the first occurrence of a character in a string. The user successfully implemented the solution by assigning the result of strchr(personName, searchChar) to the pointer searchResult. This approach effectively checks for the presence of the character 'J' in the string "Albert Johnson" and confirms its existence through a conditional statement.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with string manipulation functions in C++
  • Knowledge of pointers and memory addressing
  • Basic syntax of conditional statements in C++
NEXT STEPS
  • Explore the strchr() function in the C++ Standard Library
  • Learn about other string manipulation functions such as strstr() and strlen()
  • Study pointer arithmetic and its applications in C++
  • Investigate error handling techniques when working with pointers
USEFUL FOR

C++ developers, students learning string manipulation, and anyone interested in understanding pointer operations in C++.

carl123
Messages
55
Reaction score
0
HTML:
#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char personName[100] = "Albert Johnson";
   char searchChar = 'J';
   char* searchResult = 0;

/* Your solution goes here */

   if (searchResult != 0) {
      count << "Character found." << endl;
   }
   else {
      count << "Character not found." << endl;
   }

   return 0;
}

Please, how do I got about this? Thanks
 
Technology news on Phys.org
MHB: General rules. 11. Show some effort.

It's hard to know what you need help with or what you're struggling with. For example, do you need help with understanding the wording of the problem? Do you need to know what variable to use or datatypes?

I would use strchr() for this problem.
 
smilesofmiles said:
I would use strchr() for this problem.

Ok, I was able to figure it out. I added this:
Code:
searchResult = strchr(personName, searchChar);
and it works. Thanks!
 

Similar threads

Replies
4
Views
2K
Replies
3
Views
1K
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
73
Views
6K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 66 ·
3
Replies
66
Views
6K