Jenny_Hopkins@toby-churchill.com wrote:
Does anyone recall the subject on iRC of finding how many words you can make from mrsbarrett using grep? The final command i tried was something similar to grep -Ei "^[mrsbarrett]+$" /usr/dict/words (I have it at home, so that is from memory) This did the job, but it used all the letters as often as it pleased. Can we make it so it only uses each letter in the pattern once? Euphrasia
#include <stdlib.h> #include <stdio.h>
void fn(char* s, char* a) { int i;
if (a[0] == '\0') { printf("%s\n", s); return; }
for (i = 0; a[i] != '\0'; i++) { int slen, alen, j; char* news, *newa;
for (slen = 0; s[slen] != '\0'; slen++) ; for (alen = 0; a[alen] != '\0'; alen++) ;
news = (char*)malloc(slen+1+1); for (j = 0; j < slen; j++) news[j] = s[j]; news[slen] = a[i]; news[slen+1] = '\0';
newa = (char*)malloc(alen-1+1); for (j = 0; j < i; j++) newa[j] = a[j]; for (j = i+1; j <= alen; j++) newa[j-1] = a[j];
fn(news, newa); free(news); free(newa); } }
int main() { fn("", "mrsbarratt"); return 0; }
Output is rather long.
Alexis