Advertisement
joharido

Untitled

Mar 10th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 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.     int i = 0;
  9.     //char exitCondition[] = "quit\0";
  10.     //fgets(input, 30, stdin);
  11.     //sortArray(input);
  12.     // scanf("%s", input);
  13.     // sortArray(input);
  14.     while(1){
  15.            // fgets(input, 30, stdin);
  16.            if (strcmp(input, "quit") == 0){
  17.                break;
  18.            } else{
  19.                scanf("%s", input);
  20.                sortArray(input);
  21.            }
  22.              i++;
  23.         }
  24.     }
  25. void swap(char *ptr1, char *ptr2){
  26.     char temp = *ptr1;
  27.     *ptr1 = *ptr2;
  28.     *ptr2 = temp;
  29. }
  30. void sortArray(char *array){
  31.     int length = strlen(array);
  32.     char *ptr;
  33.     ptr = array;
  34.     int i,j,k = 0;
  35.     int smallest;
  36.     for (int i = 0; i < length -1; i++){
  37.         smallest = i;
  38.         for(int j = i + 1; j < length; j++){
  39.             if ((int)(*(array + j)) < (int)(*(array + smallest))){
  40.                 smallest = j;
  41.             }
  42.            
  43.             //*(array + i) = *(array+ smallest);
  44.         }
  45.         swap(array+smallest, array+i);
  46.     }
  47.     for (int k = 0; k < length; k++){
  48.         printf("%c", *ptr);
  49.         ptr++;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement