C Programming

Strchr Metohd in C

The use of the strchr() method in C language will be explained in this article. The strchr() method is utilized to locate a given character’s first presence inside a string. A strchr() method determines if the actual string holds specified characters. It generates a pointer value if the character is present within the string; else, it gives a null pointer. To utilize the strchr() method in the C language, we must first include the header file <string.h>.

Use the strchr() Method to Acquire the First Existence of the Character

The strchr() is an implicit string method in the C language. It is utilized to determine the first existence of a character in any string. Here, we go through the program and see how to identify the first existence of any defined character:

#include
#include
int main()
{
  const char *str = "I like to play badminton";
 
  char trgt = 'i';
  const char *rslt = str;
    rslt = strchr(rslt, trgt);
  while (rslt != NULL) {
    printf("Found '%c' in '%s'\n", trgt, rslt);
    ++rslt;
    rslt = strchr(rslt, trgt);
  }
}

First, we integrate the header file, #include <stdio.h>, for input and output. Similarly, we integrate the header file, <string.h>, to declare the strchr() method. After doing this, we start to initialize the body of the main() function. Here, we declare a string having a character data type. Next, we create a variable with the name “trgt” to store the character we want to seek in the string. This variable also has a character data type.

In this case, we assign “I” to the variable “trgt”. Another new variable is created with the name  “rslt”, and it stores the final result. This variable stores the result in the form of a string. To acquire the first occurrence of the defined character, we will apply the strchr() function. This function holds two arguments. Its first argument contains the string; however, the second argument contains the definite character.

In addition, we utilize a while loop. It checks if the value or character assigned to the “trgt” variable is present in the specified string, then the strchr() function would have to return that string which starts from the defined character. But, if the specified character is not present in the string, then the function strchr() gives NULL as an output. The function printf() is applied to print the resultant string.

In the while loop, we also do an increment in the value of the “rslt” variable. We will get the specified character at the same position if we do not do an increment. Due to this, we obtain the next existence of the character. Last, the variable “rslt” stores the resultant strings, and we get the following output:

Use the strchr() Method and an if-else Statement, to Seek a Character

Let’s look at the following instance of utilizing the strchr() method and an if-else statement to retrieve the first element in a string:

#include
#include  
int main ()  
{
    const char *str = "information technology";
    char chr;
    printf (" Original string: "%s" \n ", str);  
    printf ("Enter a character you would like to find in the string: ");  
    scanf (" %c", &chr);
    if ( strchr (str, chr) != NULL )  
    {  
        printf (" \n '%c' is found in "%s" ", chr, str);  
        }  
    else  
        printf (" \n '%c' is not found in "%s" ", chr, str);      
    return 0;    
}

At the beginning of the code, we include the required header files, “<stdio.h>” and “ <string.h>” for different purposes. Next, we start the coding in the body of the main() function. We will initialize the character pointer and declare a “chr” variable to store the character. Furthermore, we employ the printf() function to acquire the defined string.

In the next step, we get the character that we want to seek in the string from the user. The scanf() method is applied to obtain the defined character from the string. Meanwhile, we utilize the if-else condition and strchr() method to examine the string for the existence of the entered character. If that character is present in the definite string, the printf() method prints the statement. This is an indication the entered character is found in the string. Otherwise, the printf() function displays the statement that the entered character is not found in the string.

We provide the string “information technology” to the abovementioned code to look for a particular character. Then, we get the “t” character from the user as input and find the string for it. The if statement then employs the strchr() function to observe the character’s presence, “t”, and displays the given character if it is present. And we get the output because “t” is present in the string “information technology”.

Use the strchr() Method and while Loop to Find the Presence of Every Character

Consider the succeeding instance, which uses the strchr() method and the while loop to show the number of times every character appears in a defined string:

#include
#include  
int main ()  
{  
    char s[] = " I like to visit beautiful places";  
    char *pt;  
    int i = 1;
    pt = strchr (s, 'i' );
    while (pt != NULL)  
    {  
        printf (" The given character 'i' present at position %d \n", (pt - s + 1));  
        printf (" Presence of the character 'i' : %d \n", i);  
        printf (" The presence of the character 'i' in the string "%s" is "%s" \n \n", s, pt);  

        pt = strchr (pt + 1, 'i');  
        i++;  
    }  
    return 0;  
}

Before starting the coding in the body of the main() function, we have to introduce two important header files <stdio.h> and <string.h>. Now, we will initialize the string with the help of variable “s”. In the same way, a pointer variable, “pt”, is declared. We set the data type of both the string and pointer to a character. We create a new variable having an integer data type. This variable is also declared and initialized.

Moreover, we employ the strchr() method to observe the character’s presence. This function contains two parameters, including the specified string and a character. Next, we apply a while loop to check whether the pointer’s value is NULL. We utilize the printf() function to print three different statements that show the presence of the character “I” in the defined string.

In addition to this, we utilize the strchr() method once again to customize the placement of the string. Further, we increment the variable, initialized out from the while loop. And, we end the program.

Conclusion

In this article, we have thoroughly discussed the implementation of the strchr() method in C. We utilized this function to find the first existence of any character. We also see the use of the strchr() method with the while loop and if-else statement to obtain the presence of a specific character in the string. We hope you found this article helpful. Check the other Linux Hint article for more tips and articles.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content