Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. void reverse(char * str, int size) {
  5.     for(int i = 0; i < size / 2; ++i) {
  6.         char c = str[i];
  7.         str[i] = str[size - i - 1];
  8.         str[size - i - 1] = c;
  9.     }
  10. }
  11.  
  12. char * reverse_words(char * str) {
  13.     reverse(str, strlen(str));
  14.     char * p = str;
  15.     char * ps = str;
  16.    
  17.     while(*p) {
  18.         if(*p == ' ' || *p == '\0') {
  19.             reverse(ps, int(p - ps));
  20.             ps = p + 1;
  21.         }
  22.         p++;
  23.     }
  24.     reverse(ps, int(p - ps));
  25.     return str;
  26. }
  27.  
  28. int main() {
  29.     char * words = new char[128];
  30.     strcpy(words, "tests while notebook");
  31.     printf("%s\n", reverse_words((char *)words));
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement