Advertisement
joharido

Untitled

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