Some code I just found. Simple matching algorithms. I love going nuts with pointers in C

uncommon.c

#include

int main( int argc , char ** argv , char ** env )
{
if (argc != 3)
{
fprintf(stdout, "Usage: %s \n\r", argv[0]);
return -1;
}

/* find out the size for the registry */
char * registry = NULL;
registry = (char*)malloc( strlen(argv[1]) * sizeof(char) + 1);
memset(registry, 0, sizeof(registry) + 1);

char * wordX = argv[1], *wordY = argv[2], *startY = wordY;

while ( *wordX )
{
while( *wordY )
{
if (*wordX == *wordY)
break;

wordY++;
}

if (!*wordY)
{
char *rr = registry;
while(*rr) rr++;
*rr = *wordX;
}

wordY = startY;
wordX++;
}

fprintf(stdout, "output: %s\n\r", registry);

free(registry);
return 0;
}

common.c


#include

int main( int argc , char ** argv , char ** env )
{
if (argc != 3)
{
fprintf(stdout, "Usage: %s \n\r", argv[0]);
return -1;
}

/* find out the size for the registry */
char * registry = NULL;

registry = (char*)malloc( strlen(argv[1]) * sizeof(char) + 1);
memset(registry, 0, sizeof(registry) + 1);

char * wordX = argv[1], *wordY = argv[2], *startY = wordY;

while ( *wordX )
{

while (*wordY)
{
if (*wordX == *wordY)
{
char * rr = registry;

while( *rr )
{
if (*rr == *wordY)
break;
++rr;
}

if (!*rr)
{
*rr = *wordY;
break;
}
wordY++;
}
else
wordY++;
}

wordX++;
wordY = startY;
}

fprintf(stdout, "registry: %s\n\r", registry);
free(registry);

return 0;
}

Tags: