How to use strchr to print substring starting from a specific character

strchr in C : How to use strchr to print substring starting from a specific character :

In this tutorial, we will learn what is strchr in C programming language and how to use it. We will show you with an example with explanation.
strchr function can return the pointer to the first occurance of a character in a string. It takes two parameters : the input string and the character we need to look in the string. This function is defined in string header file, so we need to add #include to import this header file.

Syntax of strchr :

The syntax of strchr is as below :

void *strchr(const char *str, const char *ch);

Where,str is the given string and ch is the character to look in the string. This function will return one pointer to the first occurance of the character ch in the string str.

Example of strchr :

Now, let me show you one simple example :

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    //1
    char originalString[] = "The quick brown fox jumps over the lazy dog";

    //2
    char *c;

    //3
    char inputChar;

    //4
    printf("Given string : %s",originalString);

    //5
    printf("\nEnter a character : ");
    scanf("%c",&inputChar);

    //6
    c = strchr(originalString, inputChar);

    //7
    if (c)
    {
        printf("String starting from the input character is : %s \n", c);
    }else{
        printf("Input character is not available in the string. \n");
    };

    return 0;
}

Sample output :

Enter a character : b
String starting from the input character is : brown fox jumps over the lazy dog

c programming strchr

As you can see, the substring starting from the first occurance of character b is printed in the above example.

Explanation :

The commented numbers in the above program denote the step numbers below :

1. originalString is the given string. You can change this string or take it as user input if you want.
2. c is a character pointer.
3. inputChar is a character we want to search in the string. We will read this character from the user.
4. First of all, print out the given string to the user.
5. Ask the user to enter a character. Read and store it in the inputChar variable.
6. Now, use the strchr method to find the character pointer for the inputChar character in the string originalString.
7. Finally, first check if the input character is found in the string or not. If found, print it out, else print one error message.

Note that we are using #include at the top of the program . Without this line, this program will not compile.

Conclusion :

strchr really comes in handy whenever we want to find out the existance of a character or when we want to get the sub-string starting from a specific character. We have shown you one example how it works. You can try to extend this program by taking the string input from the user dynamically and also you can ask the user multiple times instead of one. Try to run the above program and if you have any queries , drop one comment below.

You might also like :

Leave a Reply