strchr and strchar locate character in c

26 Sep 2022 Balmiki Mandal 0 C Programming

C Programming: Finding Characters with strchr and strchar

The strchr() and strrchr() functions in C are used to locate a character in a string. strchr() locates the first occurrence of a character in a string, while strrchr() locates the last occurrence of a character in a string.

Syntax:

C Programming
char *strchr(const char *string, int c);
char *strrchr(const char *string, int c);

Parameters:

  • string: A pointer to the null-terminated string in which the search is to be performed.
  • c: The character to be searched for.

Return value:

A pointer to the first or last occurrence of the character in the string, or NULL if the character is not found.

C Programming
#include <stdio.h>
#include <string.h>

int main() {
  char string[] = "This is a test string.";

  // Find the first occurrence of the character 's' in the string.
  char *ptr = strchr(string, 's');

  // If the character is found, print its position in the string.
  if (ptr != NULL) {
    printf("The first occurrence of the character 's' is at position %d.\n", ptr - string);
  } else {
    printf("The character 's' was not found in the string.\n");
  }

  // Find the last occurrence of the character 's' in the string.
  ptr = strrchr(string, 's');

  // If the character is found, print its position in the string.
  if (ptr != NULL) {
    printf("The last occurrence of the character 's' is at position %d.\n", ptr - string);
  } else {
    printf("The character 's' was not found in the string.\n");
  }

  return 0;
}

Output:

The first occurrence of the character 's' is at position 4.
The last occurrence of the character 's' is at position 18.

The program first finds the first occurrence of the character 's' in the string using the strchr() function. If the character is found, the program prints its position in the string. Otherwise, the program prints a message indicating that the character was not found.

Next, the program finds the last occurrence of the character 's' in the string using the strrchr() function. If the character is found, the program prints its position in the string. Otherwise, the program prints a message indicating that the character was not found.

strchr() and strrchr() functions can be used in a variety of ways to search for characters in strings. For example, you could use them to:

  • Find the index of a specific character in a string.
  • Find the first or last occurrence of a character in a string.
  • Find all occurrences of a character in a string.
  • Split a string into two strings at a specific character.
  • Remove all occurrences of a character from a string.

 

The strchr() and strrchr() functions are powerful tools for searching for characters in strings. They are easy to use and can be used to solve a variety of problems.

Further Reading:


Implementation of strncat() in C

add source string at the end of destination string

comparing two string using strcmp and strncmp

Design a function to copy to copy string into destination string using strcpy and strncpy

how to implement implementation of strncpy in c

Design a function to calculate the length of the string using strlen

pre defined string based function

Design a function using my_strcpy and using my_strchr we need to select given char in given string

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.