#include #include #include #include "etc.h" /* Balaye une chaine en éliminant les mots qui se répète. Les répétitions sont enlevée en conservant la première apparition La chaine pourra être tronquée. Les répétitions sont remplacé par des blancs. Retourne un pointeur vers la nouvelle fin de la chaine */ export char *str_exclu (char *str) { while (*str != '\0'){ char mot[100]; char *newstr = str_copymot (mot,str); if (newstr != NULL){ char *pt = newstr; int len = strlen(mot); str = newstr; while ((pt = strstr(pt,mot)) != NULL){ char *fin = pt+len; char next = *fin; if (isspace(*(pt-1)) && (isspace(next) || next == '\0')){ memset (pt,' ',(int)(fin-pt)); pt--; }else{ pt = fin; } } }else{ break; } } return str_skip(str); } /* Elimine les blancs entre les mots et conserve 1 seul. Retourne la nouvelle fin de la char (-> '\0'). */ export char *str_pack (char *str) { char *dst = str; while (*str != '\0'){ if (isspace(*str)){ *dst++ = ' '; str = str_skip (str); if (*str == '\0') dst--; }else{ *dst++ = *str++; } } *dst = '\0'; return dst; } #ifdef TEST static char *tb[]={ "allo comment ca va", "allo comment ca va allo comment ca va", "allo comment ca va allo comment ca va allo comment ca va", "allo comment ca va allo0 comment0 ca0 va0", "allo comment ca va 0allo 0comment 0ca 0va", NULL }; void main() { int i=0; while (tb[i] != NULL){ char str[200]; char *end; strcpy (str,tb[i]); end = str_exclu (str); printf ("%2d - :%s:\n :%s:\n %s\n",i,tb[i],str ,end == str+strlen(str) ? "okend" : "???"); i++; } } #endif