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,907 questions

51,839 answers

573 users

How to remove the last word from a string in C

4 Answers

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

// Function to truncate the last word in a space-separated string
void removeLastWord(char *s) {
    int i = 0;
    char *p = NULL;

    // Traverse the string to find the last space
    while (s[i]) {
        if (s[i] == ' ')
            p = &s[i];
        i++;
    }

    // If a space was found, terminate the string there
    if (p != NULL)
        *p = '\0';
}

int main(void)
{
    char s[32] = "c c++ c# java python";

    // Call the function to truncate the last word
    removeLastWord(s);

    // Print the result
    printf("%s\n", s);

    return 0;
}


/*
run:

c c++ c# java

*/

 



answered Feb 24, 2017 by avibootz
edited Sep 24, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

// Function to truncate the last word in a space-separated string
void removeLastWord(char *src, char *dest) {
    int i = 0;
    char *p = NULL;
    
    strcpy(dest, src);

    // Find the last space in the string
    while (dest[i]) {
        if (dest[i] == ' ')
            p = &dest[i];
        i++;
    }

    // If a space was found, truncate the string there
    if (p != NULL)
        *p = '\0';
}

int main(void)
{
    char s[32] = "c c++ c# java python";
    char tmp[32] = "";

    // Call the function to truncate the last word
    removeLastWord(s, tmp);

    // Print the result
    printf("%s\n", tmp);


    return 0;
}



/*
run:

c c++ c# java

*/

 



answered Feb 24, 2017 by avibootz
edited Sep 24, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

// Function to remove the last word from a space-separated string
void removeLastWord(const char *src, char *dest) {
    strcpy(dest, src);

    char *p = strrchr(dest, ' ');
    if (p != NULL) {
        *p = '\0'; // Truncate at the last space
    }
}

int main(void)
{   
    char s[32] = "c c++ c# java python";
    char tmp[32] = "";

    // Call the function to remove the last word
    removeLastWord(s, tmp);

    // Print the result
    printf("%s\n", tmp);

    return 0;
}


/*
run:

c c++ c# java

*/

 



answered Feb 24, 2017 by avibootz
edited Sep 24, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

// Function to remove the last word from a space-separated string
void removeLastWord(char *str) {
    char *p = str;
    char *last_word = NULL;

    // Find the last space in the string
    while ((p = strchr(p, ' ')) != NULL) {
        last_word = p;
        p++; // Move past the space
    }

    // Truncate the string at the last space
    if (last_word != NULL) {
        *last_word = '\0';
    }
}

int main()
{
    char str[64] = "c c++ c# java php";

    // Call the function to remove the last word
    removeLastWord(str);

    // Print the result
    puts(str);

    return 0;
}



/*
run:

c c++ c# java

*/

 



answered Sep 24, 2025 by avibootz

Related questions

2 answers 160 views
1 answer 124 views
1 answer 126 views
1 answer 64 views
1 answer 92 views
1 answer 169 views
...