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

51,810 answers

573 users

How to reverse the order of the words in a string without library function in C

1 Answer

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 11, 2019 by avibootz

Related questions

1 answer 214 views
3 answers 395 views
3 answers 399 views
2 answers 176 views
176 views asked Jun 28, 2024 by avibootz
1 answer 209 views
209 views asked Jan 11, 2020 by avibootz
...