#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "C:\Users\Tal Levin\Desktop\College\C things\Homework11Pointer\Homework11Pointer\Pointer.c"
#define Numbers 10
#define Hundred 100
#define Zero 0
#define Ten 10 
#define One 1
typedef char string[Hundred];
void Nullify(char *string)
{
	//sets the string to blanks
	short count = Zero;
	while (count < Hundred-One)
	{
		*(string + count) = ' ';
		count++;
	}
	*(string + count + One) = '\0';
}
char *itoa(int *num)
{
	//turns int to char/string
	char *newString;
	char a[Hundred] = { Zero };
	newString = &a[Zero];
	int k = NumReversal(num);
	while(k>Zero)
	{
		*(newString++) = k % Ten + '0';
		k /= Ten;
	}
	*(++newString) = '\0';
	return &a[Zero];
}
int Length(char arr[])
{
	//checks the length of a string
	int count = Zero;
	while (arr[count++]!='\0');
	return count-One;
}
int SubString(char *string, char *substring,
	char *new_string)
{
	//checks how many times substring shows up in
	//main string and deletes them
	short len = Length(substring);
	short replaced = Zero;
	while (*(string))
	{
		short pos = 0;
		while (*(string + pos)!='\0'
			&& *(substring + pos)!='\0' 
			&& *(string+pos) == *(substring+pos)
			&& pos < len)
		{
			pos++;
		}
		if (pos == len)
		{
			string += len;
			replaced++;
		}
		else
		{
			*new_string = *string;
			new_string++;
			string++;
		}
	}
	return replaced;
}
int HowManyTimes(char *string, char *substring)
{
	//checks how many times substring shows up in
	//main string
	short len = Length(substring);
	short showups = Zero;
	while (*(string))
	{
		short pos = 0;
		while (*(string + pos) != '\0'
			&& *(substring + pos) != '\0'
			&& *(string + pos) == *(substring + pos)
			&& pos < len)
		{
			pos++;
		}
		if (pos == len)
		{
			string += len;
			showups++;
		}
		else
		{
			string++;
		}
	}
	return showups;
}
short HowManySeqChars(char *str,char *frequent)
{
	//checks which char appears the most and
	//how many times
	short max_count_chars = Zero;
	while (*str)
	{
		int count_chars = Zero;
		while (*(str + count_chars + One)
			&& *(str + count_chars) ==
			*(str + count_chars + One))
		{
			count_chars++;
		}
		if (count_chars > max_count_chars) {
			max_count_chars = count_chars;
			*frequent = *str;
		}
		str++;
	}
	return max_count_chars + One;
}
void StringCut(char * string, char *even, char*odd)
{
	//copies the even places to the even string
	//and the odd places to the odd string
	unsigned int counter = Zero;
	while (*(string + counter) != '\0')
	{
		if (counter % Two == Zero)
		{
			*even = *(string + counter);
			even++;
			counter++;
		}
		else
		{
			*odd = *(string + counter);
			odd++;
			counter++;
		}
	}
	*even = '\0';
	*odd = '\0';
}
Bool MemoyanLeserogin(char *even, char *odd)
{
	//checks if even places are going down, and odd are going up
	int lengthEven = Length(even);
	int lengthOdd = Length(odd);
	Bool found = True;
	for (int counter = Zero; counter < lengthEven - One; counter++)
	{
		if (*(even + counter) > *(even + counter + One))
		{
			found = False;
		}

	}
	for (int counter = Zero; counter < lengthOdd - One; counter++)
	{
		if (*(odd + counter) < *(odd + counter + One))
		{
			found = False;
		}

	}
	return found;
}
Bool MemoyanLeserogin2(char *even, char*odd)
{
	//checks if odd places are going down, and even are going up
	int lengthEven = Length(even);
	int lengthOdd = Length(odd);
	Bool found = True;
	for (int counter = Zero; counter < lengthEven - One; counter++)
	{
		if (*(even + counter) < *(even + counter + One))
		{
			found = False;
		}

	}
	for (int counter = Zero; counter < lengthOdd - One; counter++)
	{
		if (*(odd + counter) > *(odd + counter + One))
		{
			found = False;
		}

	}
	return found;
}
void isStringMemoyan(char* a, char * even, char*odd)
{
	//This function checks if the string given
	//stands by the criteria of sorted string
	StringCut(a, even, odd);
	Bool check = MemoyanLeserogin(even, odd);
	Bool check1 = MemoyanLeserogin2(even, odd);
	if (check == One)
	{
		printf("Memuyan Le Serugin\n");
	}
	if (check1 == One)
	{
		printf("Memuyan Le Serugin Hafuh\n");
	}
	if(!check1 && !check)
	{
		printf("Lo Memuyan\n");
	}
}
int DeleteSameInstances(char *string, char *sub,char *new_s)
{
	//deletes all the same words of the second string from the first
	char str[Hundred];
	char *temp = &str;
	int count = Zero;
	*temp = '\0';
	while (*(sub-One) != '\0')
	{
		Cut(sub, temp);
		count += SubString(string,temp,new_s);
		Copy(string,new_s);
		sub += Length(temp)+One;
	}
	return count;
}
/*
//-----------------------------------------------------------------
//								exe 1
//                              -----
// General : the program goes over the string and checks how many
// appearences the substring makes
// Input : a string and a word/sub-string to search for
// Process : the program takes the sub-string and checks if the
// string has the same sequence of chars in it.
// Output : The times it has shown up in the string
//-----------------------------------------------------------------
// Proggramer : Tal Levin 
// Student No : 212204176
// Date : 12.11.19
//-----------------------------------------------------------------
void main()
{
	string str1;
	string str2;
	char *origin = &str1[Zero];
	char *sub = &str2[Zero];
	Nullify(origin);
	Nullify(sub);
	printf("Enter source string: ");
	scanf("%[^\n]%*c", str1);
	printf("Enter replace string: ");
	scanf("%[^\n]%*c", str2);
	short count = HowManyTimes(origin, sub);
	printf("Number of times: %hd\n", count);
	
}
//-----------------------------------------------------------------
//								exe 2
//                              -----
// General : the program checks which char repeats itself the most
// in a string and shows how many times it shows up
// Input : a string
// Process : goes over every letter and checks the amount of it
// Output : the highest repeating char and the times it appeared
// in string.
//-----------------------------------------------------------------
// Proggramer : Tal Levin 
// Student No : 212204176
// Date : 12.11.19
//-----------------------------------------------------------------
void main()
{
	string arr;
	char f;
	short count = Zero;
	char *frequent = &f;
	char *str = &arr[Zero];
	printf("Enter string: ");
	scanf("%[^\n]%*c", str);
	count = HowManySeqChars(str, frequent);
	printf("the most frequent letter: %c \n", *frequent);
	printf("anount of times in: %hd", count);
	_getch();
}
//-----------------------------------------------------------------
//								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 : Tal Levin 
// Student No : 212204176
// Date : 12.11.19
//-----------------------------------------------------------------
void main()
{
	string arr;
	char *str = &arr[Zero];
	string arr1;
	char *str1 = &arr1[Zero];
	string arr2;
	char *str2 = &arr2[Zero];
	printf("Enter string: ");
	scanf("%[^\n]%*c", str);
	isStringMemoyan(str, str1, str2);
	_getch();
}
//-----------------------------------------------------------------
//								exe 4
//                              -----
// General : the program turns a num into string
// Input : a num
// Process : the program takes each number and adds the value in
// computer code of '0' to it, to turn it to a char
// Output : the num as a string
//-----------------------------------------------------------------
// Proggramer : Tal Levin 
// Student No : 212204176
// Date : 12.11.19
//-----------------------------------------------------------------
void main(void)
{
	char *converted;
	char b[Hundred];
	converted = &b[Zero];
	Nullify(converted);
	int *num;
	int toconvert = 2103;
	num = &toconvert;
	converted = itoa(num);
	_getch();
}
*/
//-----------------------------------------------------------------
//								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 : Tal Levin 
// Student No : 212204176
// Date : 12.11.19
//-----------------------------------------------------------------
void main()
{
	string str1;
	string str2;
	string str3;
	string str4;
	char *s = &str1[Zero];
	char *sub = &str2[Zero];
	char *new_s = &str3[Zero];
	char *origin = &str4[Zero];
	Nullify(s);
	Nullify(sub);
	Nullify(new_s);
	printf("Enter source string: ");
	scanf("%[^\n]%*c", s);
	printf("Enter replace string: ");
	scanf("%[^\n]%*c", sub);
	Copy(origin, s);
 	int changes = DeleteSameInstances(s, sub, new_s);
	printf("Origin String: %s\n",origin);
	printf("Sub string: %s\n", sub);
	printf("New string: %s\n",new_s);
	printf("Number of changes: %hd\n", changes);
	_getch();
}
