shithub: util

Download patch

ref: 4570b488dd26cd93553057ce01354dbcdcdbb192
parent: 970b9575619b8d39bac6554449ff164d6d5ceef6
author: eli <eli@cloud9>
date: Mon May 13 16:15:27 EDT 2024

anagrams update

--- a/anagrams.c
+++ b/anagrams.c
@@ -91,7 +91,7 @@
 }
 
 void
-common(void)
+common(char *input)
 {
 	dlist *cur = dict;
 	int i;
@@ -98,9 +98,11 @@
 	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;
+		if (contains(input, commonwords[i])) {
+			cur->word = strdup(commonwords[i]);
+			cur->next = calloc(1, sizeof(dlist));
+			cur = cur->next;
+		}
 	}
 }
 
@@ -116,6 +118,7 @@
 	dlist *cur;
 	item *child;
 	int i;
+	int commonwords = 0;
 
 	dict = calloc(1, sizeof(dlist));
 
@@ -126,7 +129,7 @@
 				minlen = atoi(argv[i]);
 				continue;
 			}else if (argv[i][1] == 'c'){
-				common();
+				commonwords = 1;
 				continue;
 			}
 		}else continue;
@@ -145,6 +148,9 @@
 		removechars(input, " ");
 	while(contains(input, "'"))
 		removechars(input, "'");
+
+	if (commonwords == 1)
+		common(input);
 
 	cur = dict;
 	while(cur->word != nil)