Advertisement
_fur

_fur | C Charisma Tubagus Setyobudhi

Dec 12th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAXC 7
  6.  
  7. void swap(char *a, char *b)
  8. {
  9.         char t = *a;
  10.         *a = *b;
  11.         *b = t;
  12. }
  13.  
  14. int main(void)
  15. {
  16.         int i, len;
  17.         char *in = malloc(MAXC + 1);
  18.         char *init = malloc(MAXC + 1);
  19.  
  20.         printf("Input text : ");
  21.         fgets(in, MAXC, stdin);
  22.  
  23.         // Delete whitespace
  24.         sscanf(in, "%s", init);
  25.         strncpy(in, init, MAXC);
  26.  
  27.         // String length
  28.         for (len = 0; in[len]; len++);
  29.  
  30.         do {
  31.                 for (i = 0; i < len - 1; i++) {
  32.                         swap(in + i, in + i + 1);
  33.                         printf("%s ", in);
  34.                 }
  35.  
  36.                 printf("\n");
  37.         } while (strcmp(in, init));
  38.  
  39.         free(in);
  40.         free(init);
  41.  
  42.         return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement