Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

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

2 Answers

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; 
  
    for (int j = word_index, i = word_index + strlen(word) + 1; i < strlen(s); i++, j++) {
        s[j] = s[i];
    }
      
    s[strlen(s) - strlen(word)] = '\0';
                  
    puts(s);
}
  
  
   
/*
run:
   
c python java c++ php c++
  
*/

 



answered Apr 2, 2019 by avibootz
edited Apr 3, 2019 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define ROWS 7
#define COLS 10

int main() {
    char s[] = "php c java c++ python c# java";
    char arr[ROWS][COLS];
      
    char *token = strtok(s, " ");
    int i = 0;
    while (token != NULL) {
        strcpy(arr[i++], token);
        token = strtok(NULL, " ");
    }
 
    s[0] = '\0';
    char word_to_remove[] = "java"; 
    for (int i = 0; i < ROWS; i++) {
        if (strcmp (arr[i], word_to_remove)) {
            strcat(strcat(s, arr[i]), " ");
        }
        else {
            word_to_remove[0] = '\0';
        }
        
    }
    s[strlen(s) - 1] = '\0';        

    puts(s);
     
    return 0;
}
        
       
       
        
/*
run:
         
php c c++ python c# java
    
*/

 



answered Jan 19, 2021 by avibootz
...