Check substring exists in a string in C
if (strstr(sent, word) != NULL) {
/* ... */
}
Note that strstr
returns a pointer to the start of the word in sent
if the word word
is found.
How do I check if a string contains a certain character?
By using strchr(), like this for example:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "Hi, I'm odd!";
int exclamationCheck = 0;
if(strchr(str, '!') != NULL)
{
exclamationCheck = 1;
}
printf("exclamationCheck = %d\n", exclamationCheck);
return 0;
}
Output:
exclamationCheck = 1
If you are looking for a laconic one liner, then you could follow @melpomene's approach:
int exclamationCheck = strchr(str, '!') != NULL;
If you are not allowed to use methods from the C String Library, then, as @SomeProgrammerDude suggested, you could simply iterate over the string, and if any character is the exclamation mark, as demonstrated in this example:
#include <stdio.h>
int main(void)
{
char str[] = "Hi, I'm odd";
int exclamationCheck = 0;
for(int i = 0; str[i] != '\0'; ++i)
{
if(str[i] == '!')
{
exclamationCheck = 1;
break;
}
}
printf("exclamationCheck = %d\n", exclamationCheck);
return 0;
}
Output:
exclamationCheck = 0
Notice that you could break the loop when at least one exclamation mark is found, so that you don't need to iterate over the whole string.
PS: What should main() return in C and C++? int
, not void
.
Seeing if a string contains a substring in C
Remove semicolon after the if
condition. Like
if(strstr(outputArr[j], "@") != NULL)
Because
if(strstr(outputArr[j], "@") != NULL);
is equivalent to
if(strstr(outputArr[j], "@") != NULL)
{
}
C99-6.8.3 paragraph 3:
A null statement (consisting of just a semicolon) performs no operations.
Simple way to check if a string contains another string in C?
if (strstr(request, "favicon") != NULL) {
// contains
}
How to check if string contains a substring at the end?
Your function has problems:
- you omit the return type, this is no longer supported by Standard C.
- The first
for
loop effectively erases the string pointed to bystr2
. There is no guarantee it even has a null terminator. - the
while
loop invokes undefined behavior if thestr1
does not contain a.
or if it is shorter thanstr2
. - do not hardcode ASCII values such as
97
or122
. It is neither portable not even readable. Use'a'
and'z'
or preferably the functions defined in<ctype.h>
.
Here is a simple string function for your purpose:
int str_ends_with(const char *s, const char *suffix) {
size_t slen = strlen(s);
size_t suffix_len = strlen(suffix);
return suffix_len <= slen && !strcmp(s + slen - suffix_len, suffix);
}
Checking if a string contains a substring C++
Wouldn't it be easier for strings
to have two std::string
instead of two char[20]
?
Then you would be able to say this with no problem:
if (strings.string1.find(strings.string2) != std::string::npos) {
std::cout << "found!" << '\n';
}
char
is not a class like std::string
, it doesn't have any member functions. This means char[20].find doesn't exist. std::string
is a class, however. And it does have a member function called std::string::find()
, so doing this to a std::string
is legal.
Check if a string contains a string in C++
Use std::string::find
as follows:
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
Note: "found!" will be printed if s2
is a substring of s1
, both s1
and s2
are of type std::string
.
Related Topics
Purpose of Returning by Const Value
Reason to Pass a Pointer by Reference in C++
Where Does Visual Studio Look For C++ Header Files
Displaying the #Include Hierarchy For a C++ File in Visual Studio
How to Include Libraries in Visual Studio 2012
Why Use Std::Bind Over Lambdas in C++14
C++ Array - Expression Must Have a Constant Value
Why Not Infer Template Parameter from Constructor
What Is the Windows Equivalent For En_Us.Utf-8 Locale
When Should You Use Constexpr Capability in C++11
Const VS Constexpr on Variables
How to Get Rid of 'Deprecated Conversion from String Constant to 'Char*'' Warnings in Gcc
How to Implement an Autonomous 'Self' Member Type in C++
"Incomplete Type" in Class Which Has a Member of the Same Type of the Class Itself