Create Sorter Andrin.c
This commit is contained in:
		
							parent
							
								
									4627d6994a
								
							
						
					
					
						commit
						2d00a6caf5
					
				| 
						 | 
					@ -0,0 +1,120 @@
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue