From 2d00a6caf599f99ffd4b8ab72704a2eb9466dbd5 Mon Sep 17 00:00:00 2001 From: romanschenk37 Date: Wed, 23 Mar 2022 16:29:32 +0100 Subject: [PATCH] Create Sorter Andrin.c --- P05_TicTacToe/work/sorter/Sorter Andrin.c | 120 ++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 P05_TicTacToe/work/sorter/Sorter Andrin.c diff --git a/P05_TicTacToe/work/sorter/Sorter Andrin.c b/P05_TicTacToe/work/sorter/Sorter Andrin.c new file mode 100644 index 0000000..a040203 --- /dev/null +++ b/P05_TicTacToe/work/sorter/Sorter Andrin.c @@ -0,0 +1,120 @@ +#include +#include +#include + + + +void printArray(char[10][20]); +void bubbleSort(char[10][20],size_t size); +int readIn(char[],char[10][20]); +void makeUpper(char[]); + + + +int main() { +char array[10][20]; + +for(int i=0;i<10;i++) { +char newLine[21]; +printf("Insert Word: "); +fgets(newLine,20,stdin); +while(getchar() != '\n'){} + +makeUpper(newLine); + + +if(readIn(newLine,array) == 0) { +return 0; +}else if(readIn(newLine,array) == 1){ +strncpy(array[i],newLine,20); +} +} + +bubbleSort(array,10); +printArray(array); +return 0; +} + + + +int readIn(char word[],char array[10][20]) { +if(strcmp(word,"ZZZ\n") == 0) { +return 0; +} +for(int i = 0; i < 10;i++) { +if(strcmp(word,array[i]) == 0){ +return 2; +} +} +return 1; +} + + + +void makeUpper(char newLine[]) { +for (int i = 0; i < newLine[i]!='\0'; i++) { +if(newLine[i] >= 'a' && newLine[i] <= 'z') { +newLine[i] = newLine[i] - 32; +} +} +} + + + +void printArray(char array[10][20]) { +for(int i=0;i<10;i++) { +printf("At %d: %s\n",i,array[i]); +} +} + + + +void bubbleSort(char array[10][20],size_t size) { +for(int i = 0; i < size-1;i++){ + +for(int j = 0; j < size-i-1;j++){ +char word1[20]; +char word2[20]; +strcpy(word1,array[j]); +strcpy(word2,array[j+1]); +int position = 0; +while(word1[position] == word2[position]) { +position++; +} +int w1 = word1[position]; +int w2 = word2[position]; +if(w1 > w2) { +char tmp[20]; +strcpy(tmp,word1); +strcpy(array[j],word2); +strcpy(array[j+1],tmp); +} +} +} +} + + + +void bubbleSortPointer(char array[10][20],size_t size){ +for(int i = 0; i < size-1;i++){ + +for(int j = 0; j < size-i-1;j++){ +char word1[20]; +char word2[20]; +strcpy(word1,array[j]); +strcpy(word2,array[j+1]); +int position = 0; +while(word1[position] == word2[position]) { +position++; +} +int w1 = word1[position]; +int w2 = word2[position]; +if(w1 > w2) { +char tmp[20]; +strcpy(tmp,word1); +strcpy(array[j],word2); +strcpy(array[j+1],tmp); +} +} +} +}