. Advertisement .
..3..
. Advertisement .
..4..
Manipulating strings is one of the most common tasks in many programming languages. Suppose you want to check whether or not a C string contains a substring; check out this guide. We will show you several ways to perform this task.
Method 1: Check Whether C String Contains A Substring Utilizing strstr
strstr is a component of the string facilities in the standard library in C and is specified in the header <string.h>
. This function accepts two pointer parameters char, the first of which specifies the string to search within, and the second specifies the one to find.
It locates the substring’s first starting location and then gives back the matching pointer to the parameter char. Suppose the first parameter string does not contain the substring; you will receive the pointer NULL.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
const char *tmp = "This string literal is arbitrary";
int main(int argc, char *argv[]){
char *ret;
ret = strstr(tmp, "literal");
if (ret)
printf("found substring at address %p\n", ret);
else
printf("no substring found!\n");
exit(EXIT_SUCCESS);
}
Output:
found substring at address 0x55edd2ecc014
Method 2: Check Whether C String Contains A Substring Utilizing strcasestr
The strcasestr function is not a feature of the standard library but rather an extension in glibc (also known as GNU C Library), which may be identified by the macro definition _GNU_SOURCE
.
Once identified, you can now call the function strcasestr to locate the first occurrence of the supplied substring. However, keep in mind that this method disregards both strings’ cases.
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
const char *tmp = "This string literal is arbitrary";
int main(int argc, char *argv[]){
char *ret;
ret = strcasestr(tmp, "LITERAL");
if (ret)
printf("found substring at address %p\n", ret);
else
printf("no substring found!\n");
exit(EXIT_SUCCESS);
}
Output:
found substring at address 0x55edd2ecc014
How To Copy Substring Utilizing strncpy
As an alternative, the function strncpy can be used to copy the provided substring to a different buffer. This method requires three parameters; the first one is the destination pointer char that will be used to store the copied substring.
Next, you have the second parameter as the source string, and the last one specifies the first bytes’ number to copy. Note that the final string will not have a null termination when the null byte is not present in the first bytes of the source string.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
const char *tmp = "This string literal is arbitrary";
int main(int argc, char *argv[]){
char *str = malloc(strlen(tmp));
printf("%s\n", strncpy(str, tmp, 4));
printf("%s\n", strncpy(str, tmp + 5, 10));
free(str);
exit(EXIT_SUCCESS);
}
Output:
This
string lit
The Bottom Line
So there you have the complete tutorial on how to check whether or not a C string contains a substring. We have explained different approaches to performing this task. All you have to do now is follow our guidance, keep practicing, and you will soon master the techniques.As mentioned before, manipulating strings is very common. Check out our site for more string-related guides in different programming languages, such as converting a char to a string in C++ or parsing a string in Java.
Leave a comment