How to remove part of a string from start to first occurrence of a word in C

1 Answer

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

int main() {
    char s[50] = "c python c++ java c++ php c++";
    char word[10] = "c++";
    char *p = strstr(s, word);
    int word_index = (p - s) + strlen(word) + 1; // 1 for space

    for (int j = 0, i = word_index; i < strlen(s); i++, j++) {
        s[j] = s[i];
    }
    
    s[strlen(s) - word_index] = '\0';
                
    puts(s);
}


 
/*
run:
 
c++ java c++ php c++

*/

 



answered Apr 2, 2019 by avibootz
...