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

C++

Remove all matching characters from a string using C language

Here i writing a function named void_RemoveAllChars() and is used for removing
all matching characters from the given string.

void RemoveAllChars(char*,char)

First argument is a string
Second argument is a character

Eg:
In the below example shows how to removing all the 'l' characters from the
string "llHello, world!ll".

#include <stdio.h>

void RemoveAllChars(char* String,char CharForRemoval)
{
    char *Read = String, *Write = String;
    while (*Read)
    {
        *Write = *Read++;
        Write += (*Write != CharForRemoval);
    }
    *Write = '\0';
}

int main(int argc, char *argv[])
{
    char str[]="llHello, world!ll";
    RemoveAllChars(str,"l");
    printf("Output is %s",str);
    return 0;
}

Output:
"Heo, word!"

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