L-8 MCS 275 Fri 26 Jan 2001

Below is the listing for the program we discussed in class.
/* L-8 MCS 275 concatenation of different arguments to main */

#include <stdio.h>
#include <string.h>

#define MAXLEN 80

int main(int argc, char *argv[])
/* concatenates all different arguments to one string */
{
   int i,j,found;
   char result[MAXLEN] = "";     /* do not forget to initialize ! */

   for (i = 1; i < argc; i++)    /* consider argv[i] */
   {                             /* note: argv[0] = name of program */
      found = 0;                 /* search whether argv[i] occurred */
      for (j = 1; j < i; j++)    /* compare argv[i] with argv[j] */
         if (strcmp(argv[j],argv[i]) == 0)
         {
            found = 1;           /* string already occurs */
            break;               /* leave the inner loop */
         }
      if (found == 0)            /* we found a new string */
         strcat(result,argv[i]); /* so we concatenate to the result */
   }
   printf("string of different arguments: \"%s\" \n", result);
   return 0;
}