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,974 answers

573 users

How to move the n word to the end of a string in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char* move_nth_word_to_end_of_string(const char *s, int n) {
    char *temp = malloc(strlen(s) + 1);
    strcpy(temp, s);
 
    char *parts[64];
    int count = 0;
 
    char *token = strtok(temp, " ");
    while (token != NULL) {
        parts[count++] = token;
        token = strtok(NULL, " ");
    }
 
    if (n >= 0 && n < count) {
        char *t = parts[n];
        for (int i = n; i < count - 1; i++)
            parts[i] = parts[i + 1];
        parts[count - 1] = t;
    }
 
    char *result = malloc(strlen(s) + 1);
    result[0] = '\0';
 
    for (int i = 0; i < count; i++) {
        strcat(result, parts[i]);
        if (i < count - 1) strcat(result, " ");
    }
 
    free(temp);
     
    return result;
}
 
int main() {
    char s[] = "Would you like to know more? (Explore and learn)";
    char *out = move_nth_word_to_end_of_string(s, 2);  // "like"
     
    printf("%s\n", out);
    free(out);
     
    return 0;
}
 
 
 
/*
run:
 
Would you to know more? (Explore and learn) like
 
*/

 



answered Feb 3 by avibootz
edited Feb 6 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int split_words(const char *s, char *parts[], int max_parts, char **copy_out) {
    char *copy = malloc(strlen(s) + 1);
    strcpy(copy, s);
 
    int count = 0;
    char *token = strtok(copy, " ");
    while (token && count < max_parts) {
        parts[count++] = token;
        token = strtok(NULL, " ");
    }
 
    *copy_out = copy;   // caller must free(*copy_out)
     
    return count;
}
 
char* move_nth_word_to_end_of_string(const char *s, int n) {
    char *parts[64];
    char *copy;
    int count = split_words(s, parts, 64, &copy);
 
    if (n >= 0 && n < count) {
        char *t = parts[n];
        for (int i = n; i < count - 1; i++)
            parts[i] = parts[i + 1];
        parts[count - 1] = t;
    }
 
    char *result = malloc(strlen(s) + 1);
    result[0] = '\0';
 
    for (int i = 0; i < count; i++) {
        strcat(result, parts[i]);
        if (i < count - 1) strcat(result, " ");
    }
 
    free(copy);
    return result;
}
 
int main() {
    char s[] = "Would you like to know more? (Explore and learn)";
    char *out = move_nth_word_to_end_of_string(s, 2);
     
    printf("%s\n", out);
    free(out);
     
    return 0;
}
 
 
 
/*
run:
 
Would you to know more? (Explore and learn) like
 
*/
 

 



answered Feb 3 by avibootz
edited Feb 6 by avibootz
...