if (strstr(phrase, word) != NULL) {
/* ... */
}
Note that strstr returns a pointer to the start of the word in phrase if the word word is found.
if (strstr(phrase, word) != NULL) {
/* ... */
}
Note that strstr returns a pointer to the start of the word in phrase if the word word is found.
Use strstr for this.
https://cplusplus.com/reference/cstring/strstr
So, you'd write it like..
char *sent = "this is my sample example";
char *word = "sample";
char *pch = strstr(sent, word);
if(pch)
{
...
}
Videos
I am trying to find a substring in a string. It keeps coming back true when it is actually false.
It says "ba" is in s2 but it is not.
Hi, I've been practising leetcode problems in C and realized that I am terribly weak in problems that involve strings and substrings; unlike other higher level languages like Python, I'm not aware of easy ways in C to isolate substrings, and instead having to rely on loops to iterate through the string, and it can get messy at times.
Currently a problem I'm working on is a palindrome problem: return the largest palindrome inside a given string. What are some useful ways to deal with problems involving strings in C programming? Thanks!