Advertisement
dmilicev

swap_strings_v3.c

Apr 25th, 2020
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. /*
  2.  
  3.     swap_strings_v3.c
  4.  
  5.     https://web.facebook.com/groups/c.programing/permalink/1558823557609822/
  6.  
  7.     A string consists of two strings separated by ';' .
  8.     The task is to change the order of the two stings.
  9.  
  10.     Version with pointers and without string.h .
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. char *swap(char *s)
  23. {
  24.     int i, j, length;
  25.     char *tmp, *first, *second;
  26.     char delimiter = ';';
  27.  
  28.     i=0;                                        // calculate length of s, implementation of strlen()
  29.     length=0;
  30.     while( *(s+i) != '\0' )
  31.     {
  32.         length++;
  33.         i++;
  34.     }
  35.  
  36.     tmp = (char *)malloc( length + 1 );         // reserve memory for tmp, + 1 for '\0'
  37.     if(tmp == NULL)                             // checking that it is successful
  38.     {
  39.         printf("\n malloc error for tmp ! \n");
  40.         exit(1);
  41.     }
  42.  
  43.     first = (char *)malloc( length + 1 );       // reserve memory for first, + 1 for '\0'
  44.     if(first == NULL)                           // checking that it is successful
  45.     {
  46.         printf("\n malloc error for first ! \n");
  47.         exit(1);
  48.     }
  49.  
  50.     second = (char *)malloc( length + 1 );      // reserve memory for second, + 1 for '\0'
  51.     if(second == NULL)                          // checking that it is successful
  52.     {
  53.         printf("\n malloc error for second ! \n");
  54.         exit(1);
  55.     }
  56.  
  57.     i=0;                                        // get first string from string s
  58.     j=0;
  59.     while( *(s+i) != delimiter )
  60.     {
  61.         *(first+j) = *(s+i);
  62.         i++;
  63.         j++;
  64.     }
  65.     *(first+j) = '\0';                          // finish string first
  66.  
  67.     i++;                                        // get second string from string s
  68.     j=0;
  69.     while( *(s+i) != '\0')
  70.     {
  71.         *(second+j) = *(s+i);
  72.         i++;
  73.         j++;
  74.     }
  75.     *(second+j) = '\0';                         // finish string second
  76.  
  77.     i=0;                                        // put second string in string s
  78.     j=0;
  79.     while( *(second+i) != '\0')
  80.     {
  81.         *(s+j) = *(second+i);
  82.         i++;
  83.         j++;
  84.     }
  85.  
  86.     *(s+j) = delimiter;                         // put delimiter in string s
  87.  
  88.     i=0;                                        // put first string in string s
  89.     j++;
  90.     while( *(first+i) != '\0')
  91.     {
  92.         *(s+j) = *(first+i);
  93.         i++;
  94.         j++;
  95.     }
  96.     *(s+j) = '\0';                              // finish string s
  97.  
  98.     return s;
  99. }
  100.  
  101.  
  102. int main(void)
  103. {
  104.     char str[] = "first;second";
  105.  
  106.     printf("\n before: |%s| \n", str );
  107.  
  108.     swap(str);
  109.  
  110.     printf("\n after:  |%s| \n", str );
  111.  
  112.     printf("\n again:  |%s| \n", swap(str) );
  113.  
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement