How to reverse the order of the words in a string with C

3 Answers

0 votes
#include <stdio.h>
#include <string.h>
     
int main() 
{                       
    char s[] = "c c++ php java python", reverse_words[100];
	int i_word, rw_index = 0, start_word, end_word;

	end_word = start_word = strlen(s) - 1;

    while (start_word > 0) {
        if (s[start_word] == ' ') {
            i_word = start_word + 1;
            while(i_word <= end_word) {
                reverse_words[rw_index] = s[i_word];
                i_word++;
                rw_index++;
            }
            reverse_words[rw_index++] = ' ';
            end_word = start_word - 1;
        }
        start_word--;
    }

    for(int i = 0; i <= end_word; i++) {
        reverse_words[rw_index] = s[i];
        rw_index++;
    }

    reverse_words[rw_index] = '\0'; 
	
	strcpy(s, reverse_words);

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

 



answered Dec 4, 2019 by avibootz
edited Dec 4, 2019 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void reverse_words(char s[]) {
	int i_word, rw_index = 0, start_word, end_word;
	char *reversewords = (char *)malloc((strlen(s) * sizeof(char)) + 1);
	
	end_word = start_word = strlen(s) - 1;

    while (start_word > 0) {
        if (s[start_word] == ' ') {
            i_word = start_word + 1;
            while(i_word <= end_word) {
                reversewords[rw_index] = s[i_word];
                i_word++;
                rw_index++;
            }
            reversewords[rw_index++] = ' ';
            end_word = start_word - 1;
        }
        start_word--;
    }

    for(int i = 0; i <= end_word; i++) {
        reversewords[rw_index] = s[i];
        rw_index++;
    }

    reversewords[rw_index] = '\0'; 
	strcpy(s, reversewords);
	free(reversewords);
}
     
int main() 
{                       
    char s[] = "c c++ php java python";
	
	reverse_words(s);

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

 



answered Dec 4, 2019 by avibootz
0 votes
#include <stdio.h>

void reverse_word(char *begin, char *end) { 
    char tmp; 
    while (begin < end) { 
        tmp = *begin; 
        *begin++ = *end; 
        *end-- = tmp; 
    } 
} 

void reverse_words_order(char *s) { 
    char *word_start = s; 
    char *p = s; 

    while (*p) { 
        p++; 
        if (*p == '\0') { 
            reverse_word(word_start, p - 1); 
        } 
        else if (*p == ' ') { 
            reverse_word(word_start, p - 1); 
            word_start = p + 1; 
        } 
		printf("word_begin = %s -- p = %s -- s = %s\n", word_start, p, s);
    } 
    reverse_word(s, p - 1); 
}
     
int main() 
{                       
    char s[] = "c c++ vb java python";

	reverse_words_order(s);

    puts(s);
	          
    return 0; 
} 
       
       
       
/*
run:
       
word_begin = c++ vb java python -- p =  c++ vb java python -- s = c c++ vb java python
word_begin = c++ vb java python -- p = c++ vb java python -- s = c c++ vb java python
word_begin = c++ vb java python -- p = ++ vb java python -- s = c c++ vb java python
word_begin = c++ vb java python -- p = + vb java python -- s = c c++ vb java python
word_begin = vb java python -- p =  vb java python -- s = c ++c vb java python
word_begin = vb java python -- p = vb java python -- s = c ++c vb java python
word_begin = vb java python -- p = b java python -- s = c ++c vb java python
word_begin = java python -- p =  java python -- s = c ++c bv java python
word_begin = java python -- p = java python -- s = c ++c bv java python
word_begin = java python -- p = ava python -- s = c ++c bv java python
word_begin = java python -- p = va python -- s = c ++c bv java python
word_begin = java python -- p = a python -- s = c ++c bv java python
word_begin = python -- p =  python -- s = c ++c bv avaj python
word_begin = python -- p = python -- s = c ++c bv avaj python
word_begin = python -- p = ython -- s = c ++c bv avaj python
word_begin = python -- p = thon -- s = c ++c bv avaj python
word_begin = python -- p = hon -- s = c ++c bv avaj python
word_begin = python -- p = on -- s = c ++c bv avaj python
word_begin = python -- p = n -- s = c ++c bv avaj python
word_begin = nohtyp -- p =  -- s = c ++c bv avaj nohtyp
python java vb c++ c
       
*/

 



answered Dec 5, 2019 by avibootz
edited Dec 11, 2019 by avibootz
...