#include <stdio.h>
#include "IdanStringPointersLib.h"

#define STRING_MAX_SIZE 256


//---------------------------------------------------------------------------------------
//                                        SortedInDescendingOrder
//                                        -----------------------
//
// General      : Checks whether the string characters are in descending order.
//
// Parameters   :  
//          *start_string_index - Pointer of string (char[])
//
// Return value : If the string string is descending (BOOLEAN).
//
//---------------------------------------------------------------------------------------
// Programmer : Cohen Idan
// Student No : 211675038
// Date       : 12.11.19
//---------------------------------------------------------------------------------------
BOOLEAN SortedInDescendingOrder(char *start_string_index)
{
    while (*start_string_index &&
            (*(start_string_index) >= *(start_string_index++ + ONE)));

    return !(*(start_string_index));
}

//---------------------------------------------------------------------------------------
//                                        SortedInAscendingOrder
//                                        ----------------------
//
// General      : Checks whether the string characters are in ascending order.
//
// Parameters   :  
//          *start_string_index - Pointer of string (char[])
//
// Return value : If the string characters are in ascending order (BOOLEAN).
//
//---------------------------------------------------------------------------------------
// Programmer : Cohen Idan
// Student No : 211675038
// Date       : 12.11.19
//---------------------------------------------------------------------------------------
BOOLEAN SortedInAscendingOrder(char *start_string_index)
{
    while (*start_string_index &&
            (*(start_string_index) <= *(start_string_index++ + ONE)));

    return !(*(start_string_index));
}

//---------------------------------------------------------------------------------------
//                                        CopyEvenPlacesStringAToStringB
//                                        ------------------------------
//
// General      : Transcribes the same places in the first set to another.
//
// Parameters   :  
//          *start_stringA_index - Pointer of first string (char[])
//          *start_stringB_index - Pointer of second string (char[])
//
// Return value : None.
//
//---------------------------------------------------------------------------------------
// Programmer : Cohen Idan
// Student No : 211675038
// Date       : 12.11.19
//---------------------------------------------------------------------------------------
void CopyEvenPlacesStringAToStringB(char *start_stringA_index, char *start_stringB_index)
{
    while (*start_stringA_index)
    {
        *(start_stringB_index++) = *(start_stringA_index++);
        start_stringA_index++;
    }
    *(start_stringB_index) = BACKSLASH_ZERO;
}

//---------------------------------------------------------------------------------------
//                                        MaxCountCharInString
//                                        --------------------
//
// General      : Checks which character is most often in the string.
//
// Parameters   :  
//          *start_string_index - Pointer of string (char[])
//
// Return value : The number of times the character appears most often (unsigned int).
//
//---------------------------------------------------------------------------------------
// Programmer : Cohen Idan
// Student No : 211675038
// Date       : 12.11.19
//---------------------------------------------------------------------------------------
unsigned short StringIsSortedAlternately(char *start_string_index)
{
    unsigned short flag = ZERO;
    unsigned int lenght_string = StringLenght(start_string_index);
    char even_places[lenght_string];
    char odd_places[lenght_string - ONE];
    unsigned short even_array_mode = ZERO; // 0 - Not sorted, 1 - asce sorted, 2 - desc sorted, 3 - both
    unsigned short odd_array_mode = ZERO; // 0 - Not sorted, 1 - asce sorted, 2 - desc sorted, 3 - both
    CopyEvenPlacesStringAToStringB(start_string_index, &even_places[ZERO]);
    CopyEvenPlacesStringAToStringB(start_string_index + ONE, &odd_places[ZERO]);

    even_array_mode = SortedInAscendingOrder(&even_places) * TWO + SortedInDescendingOrder(&even_places);
    odd_array_mode = SortedInAscendingOrder(&odd_places) * TWO + SortedInDescendingOrder(&odd_places);

    flag += (even_array_mode == ONE && odd_array_mode == TWO) * TWO;
    flag += (even_array_mode == TWO && odd_array_mode == ONE);
    flag += (even_array_mode == THREE && odd_array_mode == ONE);
    flag += (even_array_mode == ONE && odd_array_mode == THREE) * TWO;
    flag += (even_array_mode == THREE && odd_array_mode == THREE) * THREE;
    flag += (even_array_mode == THREE && odd_array_mode == TWO)* TWO;
    flag += (even_array_mode == ONE && odd_array_mode == TWO)* TWO; 

    return (flag);
}
//-----------------------------------------------------------------
//								exe 3
//                              -----
// General : the program checks if the string is ordered by the
//           given standard.
//
// Input   : a string.
//
// Process : goes over every even and odd space and checks if it
//           falls under the criteria of a sorted string.
//
// Output  : if it is a sorted string or not and what type.
//
//-----------------------------------------------------------------
// Proggramer : Cohen Idan 
// Student No : 211675038
// Date : 12.11.19
//-----------------------------------------------------------------
void main(void)
{

    char stringA[STRING_MAX_SIZE] = "aaaaaaa\0";
    printf("Count: %u\n", StringIsSortedAlternately(&stringA[ZERO]));
}