shithub: util

Download patch

ref: 970b9575619b8d39bac6554449ff164d6d5ceef6
parent: b57f2016149d82de9796a82826ed88f85e16a147
author: eli <eli@cloud9>
date: Sun May 12 18:23:00 EDT 2024

anagrams update

--- a/anagrams.c
+++ b/anagrams.c
@@ -22,7 +22,9 @@
 	int i, j;
 
 	for (i = 0; i < strlen(word); i++)
-		for (j = 0; j < strlen(input); j++)
+		if (word[i] == '\'')
+			continue;
+		else for (j = 0; j < strlen(input); j++)
 			if ((word[i] | 0x20) == (input[j] | 0x20)) {
 				memmove(&input[j], &input[j+1], strlen(&input[j+1])+1);
 				break;
@@ -44,7 +46,9 @@
 
 	r = 0;
 	for (i = 0; i < strlen(word); i++) {
-		for (j = 0; j < strlen(input); j++)
+		if (word[i] == '\'')
+			continue;
+		else for (j = 0; j < strlen(input); j++)
 			if ((word[i] | 0x20) == (input[j] | 0x20)) {
 				r++;
 				memmove(&input[j], &input[j+1], strlen(&input[j+1])+1);
@@ -87,31 +91,48 @@
 }
 
 void
+common(void)
+{
+	dlist *cur = dict;
+	int i;
+	char *commonwords[] = {"a", "I", "an", "at", "be", "by", "do", "he", "in", "is", "it", "my", "no", "oh", "or", "to", "us", "we", "and", "but", "for", "her", "him", "his", "its", "not", "too", "you", nil};
+
+	for(i = 0; commonwords[i] != nil; i++){
+		cur->word = strdup(commonwords[i]);
+		cur->next = calloc(1, sizeof(dlist));
+		cur = cur->next;
+	}
+}
+
+void
 main(int argc, char **argv)
 {
 	char input[8192];
 	item *items;
 	int minlen = 0;
-	char *wordsfilename;
+	char *wordsfilename = "";
 	FILE *wordsfile;
 	char word[8192];
 	dlist *cur;
 	item *child;
+	int i;
 
-	if (argc < 2) {
-USAGE:
-		fprint(2, "usage:\n\t%s [-l minlen] wordsfile\n", argv[0]);
-		exits("usage");
-	}
+	dict = calloc(1, sizeof(dlist));
 
-	wordsfilename = argv[1];
+	for(i = 1; i < argc; i++){
+		wordsfilename = argv[i];
+		if(argv[i][0] == '-'){
+			if((argv[i][1] == 'l') && (++i < argc)){
+				minlen = atoi(argv[i]);
+				continue;
+			}else if (argv[i][1] == 'c'){
+				common();
+				continue;
+			}
+		}else continue;
 
-	if (strcmp(argv[1], "-l") == 0) {
-		if (argc < 4)
-			goto USAGE;
-
-		minlen = atoi(argv[2]);
-		wordsfilename = argv[3];
+		fprint(2, "usage:\n\t%s [-l[ength] minlen] [-c[ommonenglishwords] wordsfile\n", argv[0]);
+		exits("usage");
 	}
 
 	wordsfile = fopen(wordsfilename, "r");
@@ -122,13 +143,16 @@
 	input[strcspn(input, "\n")] = '\0';
 	while(contains(input, " "))
 		removechars(input, " ");
+	while(contains(input, "'"))
+		removechars(input, "'");
 
-	dict = calloc(1, sizeof(dlist));
 	cur = dict;
+	while(cur->word != nil)
+		cur = cur->next;
 	while(fgets(word, 8192, wordsfile) != nil){
 		word[strcspn(word, "\n")] = '\0';
 
-		if (strlen(word) < minlen)
+		if(strlen(word) < minlen)
 			continue;
 
 		if(contains(input, word)){