The strchr() function is defined in the string.h header file. It helps to search the first occurrence of the specified character in the string pointed by the 'str' argument.
char *strchr(const char *str, int c); #where str should be a string
The strchr() function takes two parameters. The first argument should be a string for searching and the second one is a character which needs to be searched.
Parameter | Description | Required / Optional |
---|---|---|
str | the C string to be scanned | Required |
c | the character to be searched in str | Required |
The function returns a pointer to the first occurrence of a searched character.
Input | Return Value |
---|---|
if found | pointer to first occurrence of a character |
if not found | NULL |
#include <stdio.h>
#include <string.h>
int main()
{
const char st[] = "https://learnetutorials.com";
const char chr = '.';
char *out
out = strchr(st, chr);
printf("String after search is |%c| is - |%s|\n", chr, out);
return(0);
}
Output:
String after search is |.| is - |.learnetutorials.com|
#include <stdio.h>
#include <string.h>
int main (){
int main () {
const chr st[] = "This is just a String";
const char chr = 'u';
char *pnt;
pnt = strchr(st, chr);
printf("String starting from %c is: %s", chr, pnt);
return 0;
}
Output:
String starting from u is: ust a String