Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #define size 100
  4. char* newString(char*, int);
  5. int len_str(char* string){
  6.     int i = 0;
  7.     while (string[i] != '\0'){
  8.         i++;
  9.     }
  10.     return i;
  11. }
  12. int main(){
  13.     char* string = (char*)malloc(sizeof(char)*size);
  14.     gets(string);
  15.     int length=len_str(string);
  16.     string = newString(string, length);
  17.     printf("%s\n", string);
  18.     return 0;
  19. }
  20.  
  21. char* newString(char* string, int length){
  22.  
  23.     int j = length;
  24.     while (string[--j] == ' ');
  25.     while (string[--j] != ' ');
  26.     while (string[--j] == ' ');
  27.     while (string[--j] != ' ');
  28.     int left = j + 1, right = length - 1;
  29.     while (right >= left){
  30.         char temp = string[left];
  31.         string[left] = string[right];
  32.         string[right] = temp;
  33.         right--; left++;
  34.     }
  35.  
  36.  
  37.     int beginFirst = j + 1;
  38.     int endFirst = j + 1;
  39.     while (string[endFirst + 1] != ' '){//watch what symbol will be next
  40.         endFirst++;                     // then +1 if all is ok
  41.     }
  42.     for (int i = 0; i <= (endFirst - beginFirst) / 2; i++){
  43.         char temp = string[endFirst - i];
  44.         string[endFirst - i] = string[beginFirst + i];
  45.         string[beginFirst + i] = temp;
  46.     }
  47.  
  48.  
  49.     int endSecond = length - 1;
  50.     int beginSecond = length - 1;
  51.     while (string[beginSecond - 1] != ' '){
  52.         beginSecond--;
  53.     }for (int i = 0; i <= (endSecond - beginSecond) / 2; i++){
  54.         char temp = string[endSecond - i];
  55.         string[endSecond - i] = string[beginSecond + i];
  56.         string[beginSecond + i] = temp;
  57.     }
  58.  
  59.     return string;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement