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

#define BACKSLASH_ZERO '\0'
#define BOOLEAN unsigned short
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define ONE 1
#define TWO 2

char * LastCharOfString(char * start_string)
{
    while (*(start_string++));

    return (--start_string);
}

unsigned short StringLenght(char *start_string)
{
    return (LastCharOfString(start_string) - start_string);
}

void SwapChars(char *ptr_char1, char *ptr_char2)
{
    char temp_char = *ptr_char2;
    *ptr_char2 = *ptr_char1;
    *ptr_char1 = temp_char;
}

BOOLEAN StringCompare(char *start_stringA_index, char *start_stringB_index)
{
    while ((*start_stringA_index) && (*(start_stringA_index++) == *(start_stringB_index++)));

    return (*--start_stringA_index == *--start_stringB_index);
}

char * IndexStringBInStringA(char *start_string_a, char *start_string_b)
{
    char *ptr_string_a_temp = start_string_a;
    char temp = BACKSLASH_ZERO;
    BOOLEAN flag = FALSE;
    unsigned short textB_lenght = StringLenght(start_string_b);
    char *address = (ptr_string_a_temp + textB_lenght);
    while ((*(address - ONE)) * !flag)
    {
        SwapChars(&temp, address);
        flag = StringCompare(ptr_string_a_temp, start_string_b);
        SwapChars(&temp, address);
        address = ((++ptr_string_a_temp) + textB_lenght);
    }
    
    return (--ptr_string_a_temp);
}

unsigned int CountStringBInStringA(char *start_string_a, char *start_string_b)
{
    char *ptr_string_a_temp = start_string_a;
    char temp = BACKSLASH_ZERO;
    unsigned short count = ZERO;
    unsigned short textB_lenght = StringLenght(start_string_b);
    char *address = (ptr_string_a_temp + textB_lenght);
    while (*(address - ONE))
    {
        SwapChars(&temp, address);
        count += StringCompare(ptr_string_a_temp, start_string_b);
        SwapChars(&temp, address);
        address = ((++ptr_string_a_temp) + textB_lenght);
    }
    
    return (count);
}

void CopyString(char *start_string_index, char *start_copy_index)
{
    while (*start_copy_index)
    {
        *(start_string_index++) = *(start_copy_index++);
    }
    *(start_string_index) = BACKSLASH_ZERO;
}

void RemoveStringBFromStringA(char *start_string_a, char *start_string_b)
{
    unsigned short textB_lenght = StringLenght(start_string_b);
    char *ptr_index_stringB_in_stringA = IndexStringBInStringA(start_string_a, start_string_b);
    CopyString(ptr_index_stringB_in_stringA, ptr_index_stringB_in_stringA + textB_lenght);
}

void RemoveAllStringBFromStringA(char *start_stringA_index, char *start_stringB_index)
{
    unsigned short loop_lenght = CountStringBInStringA(start_stringA_index, start_stringB_index);
    unsigned short counter;
    for (counter = ZERO; counter < loop_lenght; counter++)
    {
        RemoveStringBFromStringA(start_stringA_index, start_stringB_index);
    }
}

//-----------------------------------------------------------------
//								exe 5
//                              -----
// General : the program takes out all the appearnces of a word,
//           from another string.
//
// Input   : two strings.
//
// Process : goes over each word in the second string, and deletes
//           the words that appear from the first one.
//
// Output  : the original string, the second string, new string, and
//           amount of changes made.
//
//-----------------------------------------------------------------
// Proggramer : Cohen Idan 
// Student No : 211675038
// Date : 12.11.19
//-----------------------------------------------------------------
void main(void)
{
    char stringA[100] = "abgjskababababablgjkljab";
    char stringB[100] = "ab";
    RemoveAllStringBFromStringA(&stringA[0], &stringB[0]);
    printf("Lenght: %s\n", stringA);

}
