C strchr() Function

beginner
5 min

C strchr() Function

Welcome to a comprehensive guide on the C strchr() function! In this lesson, we'll explore what the strchr() function is, its purpose, and how to use it effectively. By the end of this lesson, you'll be able to utilize this powerful tool in your C programming projects.

Table of Contents

  1. Introduction to the strchr() function
  2. Understanding the syntax
  3. Example 1: Basic usage
  4. Example 2: Advanced usage
  5. Pro Tips and Gotchas
  6. Quiz

1. Introduction to the strchr() function

In C programming, the strchr() function is a built-in library function that helps you find the first occurrence of a specific character within a string. It is part of the string handling functions, making it essential for manipulating and searching strings in C.


2. Understanding the syntax

The syntax of the strchr() function is as follows:

c
char *strchr(const char *str, int c);

Here, str is the string you want to search within, and c is the character you are searching for. The function returns a pointer to the found character or NULL if the character is not found.


3. Example 1: Basic usage

Let's see how to use strchr() in a simple example.

c
#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; char *ptr; ptr = strchr(str, 'o'); if (ptr != NULL) printf("First occurrence of 'o' is at position %ld\n", ptr - str + 1); else printf("'o' is not found in the string.\n"); return 0; }

In this example, we search for the character 'o' within the string "Hello, World!" and print its first occurrence position.


4. Example 2: Advanced usage

Now, let's take it a step further and use strchr() to find the position of a specific character in a more complex scenario.

c
#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World! From CodeYourCraft."; char *ptr; // Search for the first occurrence of '.' ptr = strchr(str, '.'); if (ptr != NULL) { printf("First occurrence of '.' is at position %ld\n", ptr - str + 1); // Search for the first occurrence of 'F' ptr = strchr(ptr + 1, 'F'); if (ptr != NULL) printf("First occurrence of 'F' after the first '.' is at position %ld\n", ptr - str + 1); else printf("'F' is not found after the first '.'.\n"); } else printf("'.' is not found in the string.\n"); return 0; }

In this example, we first search for the first occurrence of the '.' character and then search for the first occurrence of 'F' after the first '.'.


5. Pro Tips and Gotchas

  • Case sensitivity: The strchr() function is case-sensitive. If you search for an uppercase character within a lowercase string, it will not find a match.
  • Check for NULL: Always check if the pointer returned by strchr() is NULL before using it.
  • Memory allocation: Be careful not to modify the original string pointed by the str argument, as it may lead to unexpected behavior.

6. Quiz

Quick Quiz
Question 1 of 1

Which of the following is the correct syntax for the `strchr()` function?