I'm trying to create a simple command line test for is/is not a isogram (no repeated letters in a word), but i'm having problems with using argv as a character array.
I did some searching, this answer used strcopy, which then lead to me a more detailed malloc answer. Probably because my terrible google-foo I cannot find an example of looping through argv's characters. This code I tried is slicing the string?:
/* first argument is the number of command line arguments,
* list of command line arguments, argv[0] == function name
*/
int main(int argc, char *argv[]) {
if(argc != 2){
printf("error, bad arguments!\n");
return 1;
} else {
char* isogram = NULL;
isogram = (char*)malloc(sizeof(argv[1])+1);
strcpy(isogram, argv[1]);
for(int i=0; i<strlen(isogram); i++){
printf("isogram[%d]==%s\n", i, &isogram[i]);
}
// detect_isogram(isogram);
}
return 0;
}
Output:
gcc -o isogram isogram.c -std=c99; ./isogram testing
isogram[0]==testing
isogram[1]==esting
isogram[2]==sting
isogram[3]==ting
isogram[4]==ing
isogram[5]==ng
isogram[6]==g
Note: I tried char *isogram[]=NULL
thinking that's what I wanted to initialize, but as the website recommended this would only work with char *isogram = NULL
.
Edit: I know how to test them, I just can't get each character to compare each other. Each indexing returns the slice[i:]...
for(int i=0; i<strlen(argv[1]); i++){
for(int j=i+1; j<strlen(argv[1]); j++){
printf("isogram[%d]==%s\n", i, &argv[1][i]);
printf("isogram[%d]==%s\n", j, &argv[1][j]);
}
}