C/C++ C++ String Functions with Pointers

Click For Summary
The discussion focuses on how to assign the first occurrence of the substring "The" in the character array movieTitle to the pointer movieResult in a C++ program. Participants clarify that the strstr function is appropriate for this task, as it returns a pointer to the first occurrence of a specified substring within a string. The correct usage of strstr is highlighted, with the function call being movieResult = strstr(movieTitle, "The"). Additionally, the importance of understanding function signatures and the distinction between pointers and null values is emphasized, suggesting that using NULL instead of 0 for pointer initialization is preferable for clarity. Overall, the conversation guides users on effectively utilizing C string functions to manipulate and search within character arrays.
ineedhelpnow
Messages
649
Reaction score
0
Assign the first instance of The in movieTitle to movieResult.

Sample program:
Code:
#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char movieTitle[100] = "The Lion King";
   char* movieResult = 0;

   <STUDENT CODE>

   cout << "Movie title contains The? ";
   if (movieResult != 0) {
      cout << "Yes." << endl;
   }
   else {
      cout << "No." << endl;
   }

   return 0;
}

I have no idea how to do this. I was thinking to use either the strrchr function or strchr but I'm not sure which one. Something like movieResult=strrchr(movieTitle, ?) but I'm really confused.
 
Technology news on Phys.org
Here you are working with C strings, not C++ string objects.
As in one of your previous questions, movieTitle (an array) is a pointer to char.
For example, *movieTitle=='T' and *(movieTitle+2)=='e'.
Since movieResult is a pointer to char, the only possible value of movieResult is a pointer.
So look up the function strstr. You'll find the types of the expected parameters and the return value type.
BTW, even with pointer to char, I think it is better to use NULL instead of 0; this reinforces the idea that you're dealing with pointers.
 
This is the definition I have of strstr "strstr(str1, str2) Returns char* pointing to first occurrence of string str2 within string str1. Returns 0 if not found. "

How do I use it though?
 
If you want to call (use) a function, you must know exactly the function declaration so you can supply correct arguments. The prototype for strstr (actually there are 2 strstr's in C++; they differ only in const modifiers) is:

const strstr(char* str1,const char* str2);

For a parameter, the modifier const merely is a promise to the compiler that the parameter will not be changed by the function. For example, the compiler will disallow a statement like *str2='a' within the function body of strstr. So now to use strstr, you must supply 2 pointers to char as the arguments. For example strstr(movieResult,NULL) is a valid function call; it's not what you want, though. The return value of strstr is a pointer (non NULL) to the first occurrence of str2 in str1 or NULL if str2 is not a substring of str1. Now I leave to you the proper function call and what you should do with the return value.
Moral of the story: you must know exactly the function's signature (parameters and types) and return value to use it.
 
Last edited:
Code:
movieResult=strstr(movieTitle, "The");
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

Replies
5
Views
2K
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
4
Views
2K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
73
Views
6K
  • · Replies 118 ·
4
Replies
118
Views
9K
Replies
89
Views
6K