Check if a String Contains a Specific Character in C
To check if a string contains a specific character in C, you can use different methods such as iterating through the string with a loop or utilizing standard library functions like strchr
and strrchr
.
Example 1: Using a Loop to Check for a Character
In this example, we will traverse the string character by character using a for
loop to check if a specific character exists in the string.
main.c
#include <stdio.h>
#include <stdbool.h>
int main() {
char str[] = "Hello, World!";
char ch = 'W';
bool found = false;
// Loop through the string to check for the character
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
found = true;
break;
}
}
if (found)
printf("Character '%c' found in the string.\n", ch);
else
printf("Character '%c' not found in the string.\n", ch);
return 0;
}
Explanation:
- Declared a string variable
str
containing “Hello, World!” and a character variablech
with value'W'
. - Initialized a boolean flag
found
tofalse
to track if the character is present. - Used a
for
loop to iterate through each character ofstr
until the null terminator'\0'
is encountered. - Compared each character with
ch
; if a match is found, setfound
totrue
and exit the loop usingbreak
. - Checked the
found
flag to print the appropriate message.
Output:
Character 'W' found in the string.
Example 2: Using strchr
Function
In this example, we use the strchr()
function from <string.h>
to search for a specific character in a string. This function returns a pointer to the first occurrence of the character or NULL
if it is not found.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char ch = 'x';
// Use strchr to find the character in the string
char *ptr = strchr(str, ch);
if (ptr != NULL)
printf("Character '%c' found in the string at position %ld.\n", ch, ptr - str);
else
printf("Character '%c' not found in the string.\n", ch);
return 0;
}
Explanation:
- Included the header
<string.h>
to use thestrchr
function. - Declared the string
str
and set the search characterch
to'x'
. - Called
strchr(str, ch)
which returns a pointer to the first occurrence ofch
instr
orNULL
if not found. - Checked if the returned pointer is not
NULL
; if true, calculated the position by subtracting the base pointer ofstr
. - Printed the position if found, or a not-found message otherwise.
Output:
Character 'x' not found in the string.
Example 3: Using strrchr
to Find the Last Occurrence
In this example, we utilize the strrchr
function from <string.h>
to find the last occurrence of a specific character in the string. This can be useful when the character may appear multiple times.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "This is a test string";
char ch = 't';
// Use strrchr to find the last occurrence of the character
char *ptr = strrchr(str, ch);
if (ptr != NULL)
printf("Last occurrence of character '%c' found at position %ld.\n", ch, ptr - str);
else
printf("Character '%c' not found in the string.\n", ch);
return 0;
}
Explanation:
- Included
<string.h>
to access thestrrchr
function. - Declared a string
str
and set the search characterch
to't'
. - Called
strrchr(str, ch)
which returns a pointer to the last occurrence ofch
instr
orNULL
if it is not found. - Checked if the pointer is not
NULL
and calculated the position by subtracting the base pointer ofstr
. - Printed the position of the last occurrence if found, or a not-found message if
NULL
was returned.
Output:
Last occurrence of character 't' found at position 16.
Conclusion
In this tutorial, we explored different methods to check if a string contains a specific character in C. We learned how to: