strchr() function in C
×


strchr() function in C

28

 The strchr function in C is used to find the first occurrence of a character in a given string.

 It returns a pointer to the first occurrence of the character or NULL if the character is not found.

Syntax of strchr function in C:

char *strchr(const char *str, int character);

 str: Pointer to the null-terminated string to be searched.

 character: The character to be found in the string.

Return Value:

 If the character is found, strchr returns a pointer to the first occurrence of the character in the string.

 If the character isn't in the string, the function gives back NULL.

Example:

Here's a simple C program that demonstrates the usage of the strchr function:

// Program for strchr function in C
#include<stdio.h> 
#include<string.h> 

int main() {
    char str[] = "Hello, World!";
    char *ptr;
    char ch = 'o';

    // Find the first occurrence of 'o' in str
    ptr = strchr(str, ch);

    if (ptr != NULL) {
        printf("'%c' found at position: %ld\n", ch, ptr - str + 1);
    } else {
        printf("'%c' not found in the string.\n", ch);
    }

    return 0;
}
	

Output:

'o' found at position: 5

 This means that the character 'o' is found at the 5th position in the string "Hello, World!".

 Include the necessary header files stdio.h and string.h.

 Define a string str and a character ch.

 Use the strchr function to find the first occurrence of ch in str.

 If ptr is not NULL, print the position of ch in str.

 If ptr is NULL, print that the character was not found in the string.

Find a Character in a String

// Program to find Character in a String
#include<stdio.h> 
#include<string.h> 

int main() {
    char str[] = "Programming";
    char *ptr;
    char ch = 'm';

    ptr = strchr(str, ch);

    if (ptr != NULL) {
        printf("'%c' found at position: %ld\n", ch, ptr - str + 1);
    } else {
        printf("'%c' not found in the string.\n", ch);
    }

    return 0;
}

Output:

m' found at position: 7

Search for a Character in User Input Sentence

// Program to search a character in user input sentence
#include<stdio.h> 
#include<string.h> 

int main() {
    char sentence[100];
    char *found;
    char letter;

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    printf("Enter a character to find: ");
    scanf("%c", &letter);

    found = strchr(sentence, letter);

    if (found != NULL) {
        printf("'%c' is found at position: %ld\n", letter, found - sentence + 1);
    } else {
        printf("'%c' is not found in the sentence.\n", letter);
    }

    return 0;
}

Output:

Enter a sentence: The world is beautiful
Enter a character to find: i
'i' is found at position: 11

Check for Multiple Characters

//  Program to check Multiple characters
#include<stdio.h> 
#include<string.h> 

int main() {
    char str[] = "OpenAI is great!";
    char *ptr;
    char ch1 = 'i';
    char ch2 = 'z';

    ptr = strchr(str, ch1);

    if (ptr != NULL) {
        printf("'%c' found at position: %ld\n", ch1, ptr - str + 1);
    } else {
        printf("'%c' not found in the string.\n", ch1);
    }

    ptr = strchr(str, ch2);

    if (ptr != NULL) {
        printf("'%c' found at position: %ld\n", ch2, ptr - str + 1);
    } else {
        printf("'%c' not found in the string.\n", ch2);
    }

    return 0;
}

Output:

'i' found at position: 8
'z' not found in the string.


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments