Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

remove every appearance of char without malloc in c


void remove_char(char s[], char ch){

    unsigned int strLen = strlen(s);

    // Place ' ' where ch appears
    unsigned int cnt_ch =0 , temp=0;
    int j=0;
    while ( j < strLen ){
        if( s[j] == ch ){
            cnt_ch ++; temp ++; // count number of ch, temp will count if there is more than ch followed by
            // if another char appeared != ch
        }else if ( temp > 0 && s[j] != ch ){

            // assign the current index value to k
            int k = j;

            while ( temp -- ) k --; // reduce k by the number of the appearance of ch

            temp = cnt_ch; // assign back temp = cnt_ch

            s[k] = s[j]; // assign the value of current char to s[k] where ch appears
            s[j] = ' '; // place ' ' instead of the current char
        }
        j++;
    }
    s[strLen - cnt_ch ] ='';

    for(char * c= s; *c; c++){
        printf("%c", *c);
    }


}
 
PREVIOUS NEXT
Tagged: #remove #appearance #char #malloc
ADD COMMENT
Topic
Name
3+3 =