P05-Sorter
This commit is contained in:
		
							parent
							
								
									9df8f350f1
								
							
						
					
					
						commit
						d61f347663
					
				
										
											Binary file not shown.
										
									
								
							| 
						 | 
					@ -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]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue