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.

40,023 questions

51,972 answers

573 users

How to remove the middle word from a string in C

1 Answer

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

#define MAX_WORDS 32
#define MAX_LEN   256

void remove_middle_word(char *str) {
    char *words[MAX_WORDS];
    int count = 0;

    // Split into words
    char *token = strtok(str, " ");
    while (token != NULL && count < MAX_WORDS) {
        words[count++] = token;
        token = strtok(NULL, " ");
    }

    if (count <= 2)
        return;

    int mid = count / 2;

    // Use a separate buffer for output
    char result[MAX_LEN] = "";
    
    for (int i = 0; i < count; i++) {
        if (i == mid) continue;

        if (result[0] != '\0')
            strcat(result, " ");

        strcat(result, words[i]);
    }

    // Copy result back into original buffer
    strcpy(str, result);
}

int main() {
    char str[MAX_LEN] = "c c++ java rust python";

    remove_middle_word(str);

    printf("%s\n", str);

    return 0;
}



/*
run:

c c++ rust python

*/

 



answered Dec 24, 2025 by avibootz
...