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

</>
Copy
#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:

  1. Declared a string variable str containing “Hello, World!” and a character variable ch with value 'W'.
  2. Initialized a boolean flag found to false to track if the character is present.
  3. Used a for loop to iterate through each character of str until the null terminator '\0' is encountered.
  4. Compared each character with ch; if a match is found, set found to true and exit the loop using break.
  5. 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

</>
Copy
#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:

  1. Included the header <string.h> to use the strchr function.
  2. Declared the string str and set the search character ch to 'x'.
  3. Called strchr(str, ch) which returns a pointer to the first occurrence of ch in str or NULL if not found.
  4. Checked if the returned pointer is not NULL; if true, calculated the position by subtracting the base pointer of str.
  5. 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

</>
Copy
#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:

  1. Included <string.h> to access the strrchr function.
  2. Declared a string str and set the search character ch to 't'.
  3. Called strrchr(str, ch) which returns a pointer to the last occurrence of ch in str or NULL if it is not found.
  4. Checked if the pointer is not NULL and calculated the position by subtracting the base pointer of str.
  5. 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:

  1. Traverse a string using a for loop and a boolean flag.
  2. Utilize the strchr function to locate the first occurrence of a character.
  3. Use the strrchr function to find the last occurrence of a character.