Created
August 18, 2012 08:07
-
-
Save RustingSword/3385169 to your computer and use it in GitHub Desktop.
For Bunny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX_NUM 100 | |
#define MAX_LEN 20 | |
char strings[MAX_NUM][MAX_LEN + 1]; | |
char smallest[MAX_LEN+1], largest[MAX_LEN+1]; | |
void readWords(char strings[][MAX_LEN+1], int * num) | |
{ | |
printf("Enter word: "); | |
scanf("%s", strings[(*num)]); | |
while(strlen(strings[*num]) != 4) | |
{ | |
printf("Enter word: "); | |
scanf("%s", strings[++(*num)]); | |
} | |
} | |
void findStrings(char strings[][MAX_LEN+1], int num) | |
{ | |
int i = 0; | |
while(i < num) | |
{ | |
if(strcmp(smallest, strings[i]) > 0) | |
{ | |
memset(smallest, 0, MAX_LEN+1); | |
strncpy(smallest, strings[i], strlen(strings[i])); | |
} | |
if(strcmp(largest, strings[i]) < 0) | |
{ | |
memset(largest, 0, MAX_LEN+1); | |
strncpy(largest, strings[i], strlen(strings[i])); | |
} | |
++i; | |
} | |
} | |
int main() | |
{ | |
int numStrings = 0; | |
memset(strings, 0, MAX_NUM * (MAX_LEN + 1)); | |
memset(smallest, 'z', MAX_LEN + 1); | |
memset(largest, 0, MAX_LEN + 1); | |
printf("Finding strings\n" | |
"---------------\n"); | |
readWords(strings, &numStrings); | |
findStrings(strings, numStrings); | |
if(numStrings > 0) | |
printf("Smallest word: %s\nLargest word: %s\n", smallest, largest); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment