Advertisement
joharido

Untitled

Mar 10th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void sortArray(char *array);
  6. int main(){
  7.     char input[30];
  8.      scanf("%s", input);
  9.     while(strcmp(input, "quit") != 0){
  10.                sortArray(input);
  11.                scanf("%s", input);
  12.     }
  13. }
  14. void swap(char *ptr1, char *ptr2){
  15.     char temp = *ptr1;
  16.     *ptr1 = *ptr2;
  17.     *ptr2 = temp;
  18. }
  19. void sortArray(char *array){
  20.     int length = strlen(array);
  21.     char *ptr;
  22.     ptr = array;
  23.     int i,j,k = 0;
  24.     int smallest;
  25.     for (int i = 0; i < length -1; i++){
  26.         smallest = i;
  27.         for(int j = i + 1; j < length; j++){
  28.             if ((int)(*(array + j)) < (int)(*(array + smallest))){
  29.                 smallest = j;
  30.             }
  31.         }
  32.         swap(array+smallest, array+i);
  33.     }
  34.     for (int k = 0; k < length; k++){
  35.         if (*(ptr+k) =='\n'){
  36.             printf("%c", '\n');
  37.         } else{
  38.             printf("%c", *ptr);
  39.         }
  40.         //printf("%c", *ptr);
  41.         ptr++;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement