C - strchr() function
C strchr() function - string scanning operation
The strchr() function is used to find the first occurrence of a character in a given string or it returns NULL if the character is not found. The null terminating character is included in the search.
Use strchr() function when:
- Searching for a specific character in a string, such as finding a delimiter or special character.
- Locating the position of a character to perform operations based on its position, like extracting substrings.
- Checking if a character exists within a string before proceeding with further operations.
Syntax:
char *strchr(const char *str, int c)
Parameters:
Name | Description | Required /Optional |
---|---|---|
str | Null-terminated source string. | Required |
c | Character to be located. | Required |
Return value from strchr()
Upon completion, strchr() shall return a pointer to the byte, or a null pointer if the byte was not found.
Examples: strchr() function
Example 1: Locate the first occurrence of various characters
In this example, strchr() is used to locate the first occurrence of various characters ('C', 'a', 'g', and '.') within the string "C Programming.". Each character’s position is calculated by subtracting the string's starting address from the pointer returned by strchr(). The program then prints each character's first position in the string.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char str1[SIZE] = "C Programming.";
int ch = 'C';
char* ptr = strchr(str1, ch);
printf( "The first occurrence of %c in '%s' is '%d'\n",
ch, str1, ptr - str1 + 1 );
ch = 'a';
ptr = strchr(str1, ch);
printf( "The first occurrence of %c in '%s' is '%d'\n",
ch, str1, ptr - str1 + 1 );
ch = 'g';
ptr = strchr(str1, ch);
printf( "The first occurrence of %c in '%s' is '%d'\n",
ch, str1, ptr - str1 + 1 );
ch = '.';
ptr = strchr(str1, ch);
printf( "The first occurrence of %c in '%s' is '%d'\n",
ch, str1, ptr - str1 + 1 );
}
Output:
The first occurrence of C in 'C Programming.' is '1' The first occurrence of a in 'C Programming.' is '8' The first occurrence of g in 'C Programming.' is '6' The first occurrence of . in 'C Programming.' is '14'
Example 2: Finding the position of a character in a URL
This example demonstrates using strchr() to find the first occurrence of '/' in a URL, which could indicate the start of the path after the domain.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a URL string
char url[] = "https://www.example.com/page";
// Find the first occurrence of '/' in the URL
char *ptr = strchr(url, '/');
if (ptr != NULL) {
// Calculate the position of '/'
int position = ptr - url + 1;
printf("First occurrence of '/' in URL is at position %d\n", position);
} else {
printf("Character '/' not found in URL.\n");
}
return 0;
}
Output:
First occurrence of '/' in URL is at position 7
Explanation:
- A URL string is defined, and strchr() is used to find the first '/' in it.
- If found, the program calculates and prints the position of the first '/', which can help separate the protocol from the path.
- If the character is not found, a message is printed.
Example 3: Searching for a space in a sentence
This example uses strchr() to locate the first space in a sentence, helping find the position where the first word ends.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a sentence
char sentence[] = "Hello world from C programming";
// Find the first occurrence of space (' ')
char *ptr = strchr(sentence, ' ');
if (ptr != NULL) {
// Calculate and print the position of the first space
int position = ptr - sentence + 1;
printf("The first space is at position %d\n", position);
} else {
printf("No space found in the sentence.\n");
}
return 0;
}
Output:
The first space is at position 6
Explanation:
- A sentence is defined, and strchr() locates the first space (' ').
- The position of the first space is printed, allowing the user to identify where the first word ends.
- If no space is found, it indicates that the sentence is a single word.
C Programming Code Editor:
Previous C Programming: C strncat()
Next C Programming:C strcmp()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-strchr.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics