P05-Sorter

This commit is contained in:
Didnt Read Readme 2022-03-23 16:14:51 +01:00
parent 9df8f350f1
commit d61f347663
3 changed files with 62 additions and 0 deletions

View File

BIN
P05_TicTacToe/work/sorter/sorter Executable file

Binary file not shown.

View File

@ -0,0 +1,62 @@
#include <string.h>
#include <stdio.h>
#define numWords 10
#define lenWords 20
int toupper(int ch);
int main(void)
{
char words[numWords][lenWords] = {0};
int readFinished = 0;
for(int i = 0; i < numWords; i++){
char word[lenWords] = {};
if(readFinished == 0){
int newWord = 0;
while(newWord == 0 && readFinished == 0){
printf("%d. Next Word: ", i);
scanf("%s", word);
for(int k = 0; k < lenWords; k++)
{
word[k] = toupper(word[k]);
}
if(strcmp("ZZZ", word) == 0){
readFinished = 1;
printf("Finished reading...\n");
}
else{
newWord = 1;
for(int j = 0; j < i; j++){
if(strcmp(word, words[j]) == 0){
newWord = 0;
printf("Word was already in List.\n");
}
}
if(newWord == 1){
strcpy(words[i], word);
}
}
}
}
}
char temp[lenWords];
for(int i=0;i<numWords;i++)
for(int j=i+1;j<numWords;j++){
if(strcmp(words[i],words[j])>0){
strcpy(temp,words[i]);
strcpy(words[i],words[j]);
strcpy(words[j],temp);
}
}
printf("Sorted Words:\n");
for(int i = 0; i < numWords; i++){
printf("%s\n", words[i]);
}
}