Hey everyone,
I'm a C programming newbie (and recently registered with this website as well) and I'm having a bit of a coding problem. I'm designing a program that takes command line number inputs in the form "./remove_duplicates 5 6 6 5" and removes the duplicate numbers (the output here would be "5 6").
My only problem is when I try to restrict the input possibilities---I only want spaces and numbers allowed. Any letters for example, be it standalone or tagged on the end of a number, should get an invalid input message and return 1. That's the part I'm struggling with. Below is my attempt, I'd greatly appreciate any help.
Thanks,
Noah
Code:
// function: to receive a string of command line numbers and return the same string of numbers with duplicates removed
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *arg[]) {
int i, j, printmeplz, l;
char number[l];
printf("\n");
for (i=1; i<argc; i++) {
l=strlen(arg[i]);
number[l]=*arg[i];
for (j=1; j<l; j++) {
if (isdigit(number[j])==0) {
printf("INVLAID INPUT\n\n");
return 1;
}
}
}
if (argc < 2)
{
printf("\nThe program receives command-line inputs---numbers separated by a space---and returns the same numbers but with all the duplicates removed from the string.)\n\n");
}
else {
for (i=1; i<argc; i++) {
printmeplz=1;
for (j=1; j<i; j++) {
if (strcmp(arg[i],arg[j])==0) {
printmeplz=0;
j=i;
}
else {
printmeplz=1;
}
}
if (printmeplz==1) {
printf("%d ", atoi(arg[i]));
}
}
printf("\n\n");
}
return 0;
}