C

Example Of The C Strchr() Function

strchr() looks for the occurrence of a specified character in the given string and returns a pointer to it.

The strchr() function in C

char *strchr(const char *str, int ch)

str – The string that will be searched for the character.
ch – The character to be found in the string str.

Strchr’s return value ()

It returns the pointer to the first occurrence of the specified character in the given string, which means that if we display the string value of the pointer, the part of the input string beginning with the first occurrence of the specified character should be displayed.

In C, consider the strchr() function.

In this example, we have a string and are using the strchr() function to find the character ‘u’ within it. When we displayed the string value of the function’s returned pointer, it displayed the string beginning with the character ‘u’, because the function’s pointer points to the character ‘u’ in the string.

#include <stdio.h>
#include <string.h>
int main () {
   const char str[] = "This is just a String"; 
   const char ch = 'u'; 
   char *p;
   p = strchr(str, ch);
   printf("String starting from %c is: %s", ch, p);
   return 0;
}

Output:

String starting from u is: ust a String

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button