strrchr(): searching for matching character

#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strrchr(str,'e');
   printf("Position :%d",p-str+1);
   return 0;
}
The output of above code is here
Position :18
In above code, by subtracting the pointer of main character and by adding 1 to it , we will get the position of matching character related to main string.

As we get a pointer by using strrchr() , so we can display the character value so rest of the string starting from searched character will be returned.
#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strrchr(str,'e');
   printf("Position :%d , %s",p-str+1,p);
   return 0;
}
Output is here
Position :18, et
We can search and get the pointer of last occurance of mathching character by using strrchr() string function.
strrchr(main_string, search_string);
Output is pointer of last occurrence of matching character. Returns null if searched character is not found.

Example 1: Finding the Last Occurrence with User Input

This example demonstrates how to find the last occurrence of a character in a user-provided string using strrchr().

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

int main() {
    char str[100], ch;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    
    printf("Enter a character to find: ");
    scanf("%c", &ch);
    
    char *pos = strrchr(str, ch);
    if (pos) {
        printf("Last occurrence of '%c' found at position: %ld\n", ch, pos - str);
    } else {
        printf("Character '%c' not found in the string.\n", ch);
    }
    
    return 0;
}
Output:
Enter a string: Hello, plus2net!
Enter a character to find: l
Last occurrence of 'l' found at position: 10

Example 2: Discarding Items Conditionally

Use strrchr() to truncate a string after the last occurrence of a specific character.

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

int main() {
    char str[] = "file/path/filename.txt";
    char *pos = strrchr(str, '/');
    
    if (pos) {
        *(pos + 1) = '\0';  // Keep everything before the last '/'
        printf("Truncated string: %s\n", str);
    } else {
        printf("Character not found!\n");
    }
    
    return 0;
}
Output:
Truncated string: file/path/

Example 3: Counting Occurrences of a Character

This example uses a loop with strrchr() to count how many times a character appears in a string.

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

int count_char(char *str, char ch) {
    int count = 0;
    char *pos = strrchr(str, ch);
    
    while (pos) {
        count++;
        *pos = '\0';  // Remove the last occurrence and continue searching
        pos = strrchr(str, ch);
    }
    
    return count;
}

int main() {
    char str[] = "plus2net is a great site for tutorials, plus2net is helpful.";
    char ch = 't';
    int count = count_char(str, ch);
    printf("The character '%c' appears %d times.\n", ch, count);
    
    return 0;
}
Output:
The character 't' appears 6 times.


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer