0

Using the function strchr is it possible to search for a substring in a string instead of a character?

Example:

Instead of this :

int r=strchr("Hello World",'W');

Can this be used :

int r=strchr("Hello World","World");
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Nnn
  • 191
  • 3
  • 9

3 Answers3

3

Using the function 'strchr()' is it possible to search for a 'substring' in a string instead of a 'character'?

Example :

Instead of this : int r=strchr("Hello World",'W');

Can this be used : int r=strchr("Hello World","World");

No, that's what strstr() is for.

Note also that strchr() does not return int, it returns a char * pointer to the character searched for, or NULL if not found. Compiler warnings exist for a reason...

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
2

Nope. You can use strstr for that

char *substring = strstr("Hello World","World");
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nodarius
  • 366
  • 4
  • 15
1

Use strstr for this.

Use this link for reference

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

Sridhar Rajan
  • 105
  • 3
  • 8