How to remove the last word from a string in C

5 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
0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// Remove trailing spaces
static void rtrim(char *s) {
    int len = strlen(s);
    while (len > 0 && isspace((unsigned char)s[len - 1])) {
        s[--len] = '\0';
    }
}

// Remove the last word from a space-separated string
void removeLastWord(char *s) {
    rtrim(s);  // First remove trailing spaces

    int len = strlen(s);
    if (len == 0)
        return;

    // Scan backward to find the last space
    int i = len - 1;
    while (i >= 0 && !isspace((unsigned char)s[i])) {
        i--;
    }

    // If a space was found, truncate there
    if (i >= 0)
        s[i] = '\0';
}

int main(void)
{
    char s[32] = "c c++ c# java python";
    removeLastWord(s);
    printf("1. %s\n", s);

    strcpy(s, "");
    removeLastWord(s);
    printf("2. %s\n", s);

    strcpy(s, "c");
    removeLastWord(s);
    printf("3. %s\n", s);

    strcpy(s, "c# java python ");
    removeLastWord(s);
    printf("4. %s\n", s);

    strcpy(s, "  ");
    removeLastWord(s);
    printf("5. %s\n", s);

    return 0;
}

 



answered Mar 27 by avibootz
...