C++ String Functions with Pointers

In summary: 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. "The function strstr takes 2 pointers to char as arguments. It searches the first pointer, movieResult, for the string "The" and returns a pointer to the first occurrence of that string.
  • #1
ineedhelpnow
651
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
  • #2
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.
 
  • #3
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?
 
  • #4
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:
  • #5
Code:
movieResult=strstr(movieTitle, "The");
 

FAQ: C++ String Functions with Pointers

What are string functions in C++?

C++ string functions are pre-defined functions that can be used to manipulate strings, which are sequences of characters. They allow for tasks such as finding the length of a string, concatenating strings, and comparing strings.

How do I use string functions with pointers in C++?

To use string functions with pointers in C++, you need to declare a pointer variable that points to a string. Then, you can use the arrow operator (->) to access the string functions for that pointer variable.

What is the difference between using string functions with pointers and without pointers?

Using string functions with pointers allows for more efficient memory usage and manipulation of strings, as the pointer points directly to the string in memory rather than creating a copy. It also allows for more flexibility in modifying the string.

Can I use string functions with pointers for dynamic memory allocation?

Yes, you can use string functions with pointers for dynamic memory allocation. This allows for the creation and manipulation of strings of varying lengths at runtime, without having to pre-allocate memory.

Are there any precautions I need to take when using string functions with pointers in C++?

Yes, when using string functions with pointers in C++, you need to ensure that the pointer is pointing to a valid string. If the pointer is null or pointing to a string that has not been properly allocated, it can result in errors or unexpected behavior. It is important to properly allocate and deallocate memory when using string functions with pointers.

Back
Top