x This domain is for sale. If you intrested, Please contact : webspeckle@gmail.com

Get the last occurrence of a string using C language


char * strrstr ( const char *, const char * );

str1     The input string
str2     This string containing the sequence of characters to match.

Return Value
A pointer to the last occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

Eg:
#include <stdio.h>
char *strrstr(char *MainString,char *SubString)
{
    char *Read,*CharPos;
    int SubStringLength=strlen(SubString);
    CharPos=strrchr(MainString,*SubString);
    if(CharPos != NULL)
    {
        for(Read=CharPos; Read >= MainString; Read--)
        {
            if (strncmp(Read, SubString, SubStringLength) == 0)
            {
                return Read;
            }
        }
    }
    return NULL;
}
int main(int argc, char *argv[])
{
    char * ptr;
    ptr= strrstr("webspecklelearnandshareknowledge","know");
    printf ("%s",ptr);
    return 0;
}

Output :
knowledge

No comments:

Post a Comment