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.

Answer from nneonneo on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › get-a-substring-in-c
Get a Substring in C - GeeksforGeeks
July 23, 2025 - In this article, we will learn how to extract a substring using a C program. The simplest method to get a substring from a larger string is by using strncpy() function.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
strstr() — Locate Substring
We cannot provide a description for this page right now
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strstr.php
C Language: strstr function (Search String for Substring)
The syntax for the strstr function in the C Language is: ... A string to search within. ... The substring that you want to find.
🌐
Sanfoundry
sanfoundry.com › c-program-substring-string
C Program to Check if the Substring is Present in the Given String - Sanfoundry
May 13, 2022 - To check substring is present in the given string. While loop is used to compute the str[] and search[] array variable value is not equal to null. ... If the condition is true then execute the iteration of the loop.
🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › c-program-to-search-substring-in-a-given-string
C Program to Search Substring in a given String
If the substring is found, the flag variable is set to 1 else it is set to 0. At the end of the program, if the flag is 1, program prints a message “Substring is found in the entered string” else program prints this message “Substring is found in the entered string”. #include<stdio.h> int main() { char str[80], substr[10]; int count1 = 0, count2 = 0, i, j, flag; //user enters the string printf("Enter a string: "); scanf("%s", str); //user enters the substring printf("Enter a substring to search: "); scanf("%s", substr); //'\0' represents the end of the string while (str[count1] != '\0'
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library strstr function
C Standard Library strstr Function
August 29, 2012 - The function returns a pointer to the first characters of str_2 in str_1 or null pointer if str_2 is not found in str_1. If str_2 is found as an empty string, str_1 is returned. Following is the C library program that demonstrates the substring presence within a main string using the function strstr().
🌐
w3resource
w3resource.com › c-programming-exercises › string › c-string-exercise-14.php
C : Check if a given substring is present in a given string
#include <stdio.h> int main() { char str[80], search[20]; // Declare character arrays for the main string and the substring to search int c1 = 0, c2 = 0, i, j, flg; // Declare variables for counting, iteration, and flag for substring check printf("\n\nCheck whether a given substring is present in the given string :\n"); // Display information about the task printf("-------------------------------------------------------------------\n"); printf("Input the string : "); fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard) printf("Input the substring to be searched :
Find elsewhere
🌐
Programming Simplified
programmingsimplified.com › c › source-code › c-substring
C substring, substring in C | Programming Simplified
C substring program to find substring of a string and its all substrings. A substring is itself a string that is part of a longer string.
🌐
YouTube
youtube.com › watch
C Program to find substring in a string - YouTube
Learn how to find a substring in a given string in C languageLike, Comments, Share and SUBSCRIBEvisit www.mysirg.com for all FREE videos
Published   December 12, 2017
🌐
Techcrashcourse
techcrashcourse.com › 2014 › 11 › c-program-find-substring-of-string.html
C Program to Fetch a Substring From a String
Here we are using a user-defined function getSubString, which takes source string pointer, destination string pointer, starting index and substring length(num) as input arguments and returns substring. We copy num characters from source string starting from index till index + num.
🌐
Reddit
reddit.com › r/learnprogramming › how to deal with string/substring problems? (c language)
r/learnprogramming on Reddit: How to deal with string/substring problems? (C language)
May 25, 2021 -

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!

Top answer
1 of 3
2
You can use strstr to find substrings. Take a look at string.h, there are a lot of useful functions there, some that are not that very known.
2 of 3
2
Personally I love doing string problems in C but a lot of people hate it, for good reason. http://www.asciitable.com/ make lowercase (i think thats right) c = c ^ 32; use pointers when iterating through string char temp = *s;``` instead of for(int i=0; i < strlen(s); i++) char temp = s[i]; * If you want to count the occurrence of different characters, do: . int* occurrence(char *str) { int occ[128] = {0}; for(; *str; str++) occ[*str]++; return occ; } int main(){ char *str = "hello world! or something."; int occ = occurrence(str); for(char i = 32; i < 128; i++) //consult ascii table printf("%c: %d\n", i, occ[i]); return 0; } you can see me use it here: https://leetcode.com/problems/longest-substring-without-repeating-characters/ int lengthOfLongestSubstring(char *s) { int longest = 0, start = 0, arr[128] = {0}, *lastSeen; for(int i = 1; *s; i++, s++) { lastSeen = &arr[*s]; //good stuff if(*lastSeen > start) start = *lastSeen; if(longest < i - start) longest = i - start; *lastSeen = i; } return longest; } * keep track of your pointers if youre using the loops I showed you * use string.h read the documentation. * dont use what ive said if it makes your program unreadable. But go ahead if its a leetcode problem. If you want I have some time this week I can help you out for an hour if that sounds like something you want? There's a lot that goes into it, so if some of these things arent making sense feel free to reach out.
🌐
Quora
quora.com › How-do-you-extract-a-substring-from-a-string-in-C
How to extract a substring from a string in C - Quora
The way to extract a substring is to copy a range of the array to a new char array, and then making sure to put a NULCHAR [code ]'\0' [/code]as the last char of the substring. You do this by char pointer offsets,...
🌐
Delft Stack
delftstack.com › home › howto › string contains in c
How to Check if String Contains Substring in C | Delft Stack
March 12, 2025 - One of the most straightforward ways to check if a string contains a substring in C is by using the strstr function. This function is part of the C standard library and is included in the <string.h> header file.
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cstring › strstr
C++ cstring strstr() - Locate Substring | Vultr Docs
November 13, 2024 - This function is part of the <cstring> ... and data validation. In this article, you will learn how to skillfully use the strstr() function to locate a substring within a string....
🌐
Cplusplus
cplusplus.com › reference › string › string › find
std::string.find()
Searches the string for the first occurrence of the sequence specified by its arguments. When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos. Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match.
🌐
Quora
quora.com › How-can-I-check-whether-one-string-is-a-sub-string-of-another-string-in-C-program
How to check whether one string is a sub-string of another string in C program - Quora
Answer (1 of 7): strstr() function from string.h library [code]#include #include int main(void) { char st1[] = "this is a string"; char st2[] = "is"; if (strstr(st1, st2) == NULL) printf("%s not in %s", st2, st1); else printf("%s", strstr(st1...
🌐
Reddit
reddit.com › r/c_programming › how can i get a substring from a string?
How can I get a substring from a string? : r/C_Programming
December 4, 2018 - If it is necessary to separate the prefix for further processing, the %s modifier can be used to store it into a second array with the same size (or larger) than the original. ... Try strchr() with pointer arithmetic or scan the string in loop, copying interesting characters to the other string.
🌐
Cplusplus
cplusplus.com › reference › cstring › strstr
Cplusplus
A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1. In C, this function is only declared as: char * strstr ( const char *, const char * ); instead of the two overloaded versions provided ...