how do i stop this code from printing the same things twice ./run has has instead of it printing
The same arguments have been found has at 1, 2
it prints
The same arguments have been found has at 1, 2
The same arguments have been found has at 2, 1
#include <string.h>
int main(int argc, char *argv[])
{
int r, k;
printf("Command Line Arguments: ");
for (r = 1; r < argc; r )
{
printf("%s ", argv[r]);
}
printf("\n");
for (r = 1; r < argc; r )
{
for (k = 1; k < argc; k )
{
if (r != k && strcmp(argv[r], argv[k]) == 0)
{
printf("The same arguments have been found %s at %d, %d\n", argv[r], r, k);
}
}
}
return 0;
}
CodePudding user response:
Simple; don't compare an argument to another argument that has already been compared to everything.
Instead of this:
for (k = 1; k < argc; k )
do this:
for (k = r 1; k < argc; k )
CodePudding user response:
You code has a bug first
you forgot
#include <stdio.h>
second, the only time it prints twice is when you input two same arguments because of your condition checking
if you input 1, 2, two different arguments
it should only print once