/* Example: bubble sort strings in array */

#include <stdio.h>  /* Need for standard I/O functions */
#include <string.h> /* Need for string functions */
#include <stdlib.h> /* Needed for malloc */

#define NUM 25   /* number of strings */
#define LEN 1000  /* max length of each string */

main()
{
  char *Strings[NUM];
  char temp[LEN];
  int i,j,k;
  int length;


  printf("Please enter %d strings, one per line:\n", NUM);

  /* Write a for loop here to read NUM strings, using fgets(). */

  for (i=0; i<NUM; i++) {

    /* Read one line of input into a temp string that is long enough (LEN long) */

   // temp = malloc(LEN); //allocates memory for temp

    fgets(temp, LEN, stdin);

    /* Allocate memory space for String[i] that is only large enough to copy the
       string just read into it.  Suppose the length of the string read into
       temp is "length", then you can use the following: */

       length = strlen(temp);

    Strings[i] = malloc(length+1); /* Plus one for the NULL at end */

       for(j=0;j<strlen(temp);j++){
         *(Strings[i]+j)= *(&temp[0]+j); //doing character by character. 
      //Left hand side: takes the memory address of the character 
      //sequence at position i
      //adds j to it (this will increment it by one because the assembler knowns that 
      //we're dealing with char here) 
      //and then dereferences it with the * so that we're assigning
      //the memory that that location points to to the RHS. 
      //As for the RHS, we could use temp instead of &temp[0]
      //either way, we're referring to the memory position of the 
      //first character in the temp array. We're adding j to the memory
      //position of the first index, and then dereferencing that to get the 
      //value at that position.

       }

     }

     puts("\nHere are the strings in the order you entered:");

  /* Write a for loop here to print all the strings. */

     for (i=0; i<NUM; i++){
      printf("%s",Strings[i]);
    }

 /* Bubble sort */
  /* Write code here to bubble sort the strings in ascending alphabetical order*/


 /* 
    Your code must meet the following requirements:
    (i) The comparison of two strings must be done by strcmp() or strncmp().
    
    (ii) The swap of two strings must be done by swapping pointer values.
    You must not swap two string using strcpy()/strncpy() or using your 
    own loop to swap them a character at a time.
  */

    

    for(i=0;i<NUM;i++){
      for(j=NUM-1;j>i;j--){

        if(strcmp(*(&Strings[0]+j),*(&Strings[0]+j-1))<0){


          char *temporaryHolding=Strings[j]; //Here, we're creating a new pointer tempHolding 
          //that points to the first position of the array at Strings[j].
         
          //printf("%s.",temporaryHolding);

          Strings[j]=Strings[j-1]; //Here, we're replacing the pointer at 
          //position j to be the one at j-1

          Strings[j-1]=temporaryHolding;
          //finally, we're setting the pointer at position j-1 to be the one that was at 
          //position j. By doing it this way, we haven't had to copy, just rearrange pointers. 


          
          /*
          for(k=0;k<strlen(Strings[j]);k++){
            *(temporaryHolding=*(Strings[j]+k);
            *(Strings[j]+k)=*(Strings[j-1]+k);
            *(Strings[j-1]+k)=*(temporaryHolding+k);
          }
          */

        }else{
          //no swap is needed
        }

      }
    }



  /* Output sorted list */

    puts("\nIn alphabetical order, the strings are:");     
  /* Write a for loop here to print all the strings. */


    for (i=0; i<NUM; i++){
      printf("%s",Strings[i]);
    }

  /* Write a loop here to use free() to free up space allocated 
     for all of the strings above.  */

  //for(...)


    return 0;
  }
