简体   繁体   中英

How to check last character in command line arguements?

So im trying to count the number of files in the command line, heres what ive got so far

# include <stdio.h>
# include <stdlib.h>

int main(int argc, char* argv[]){
int cCount = 0;
int cHeadCount = 0;
int objCount = 0; 
int makeCount = 0;
int othCount =0;

for(int i = 1; i< argc; i++){
    /*
    if argv[i] last char is c 
        cCount++;

    if argv[i] last char is h 
        cHeadCount++;

    and so on for the rest

    */
printf("C Source: %d\n", cCount);
printf("C Header: %d\n", cHeadCount);
printf("Object: %d\n", objCount);
printf("Make: %d\n", makeCount);
printf("Other: %d\n", othCount);
}

so if you type in something like $ ./fm main.c main.o sub.c sub.o you should get

C source: 2
C header: 0
Object: 2
Make: 0
Other: 0

What i need help is the if statements inside the for loop. BTW is the for loop correct? Is there a function that will return the last character of a string? From what Ive seen, I dont seem to recall one but I could be very wrong.

If im going about this wrong, or on right track, please tell me. Any help appreciated.

EDIT:

Heres what I have inside the for loop:

for(int i = 1; i < argc; i++){
    int len = strlen (argv[i]);

    if ((argv[i][len - 2] != '.') ){
        if((strcmp(argv[i], "Makefile")==0) || (strcmp(argv[i], "makefile")==0)){
        makeCount++;
    }else{
        othCount++;
        continue; //if i take this out, it counts `other` objects wrong too
    }
    }

    if(argv[i][len - 1] == 'c'){
        cCount++;

    }

    else if(argv[i][len - 1] == 'h'){
        cHeadCount++;

    }

    else if(argv[i][len - 1] == 'o'){
        objCount++;

    }

    else {
        othCount++;           
    }       

}

So the problem now is that its not recognizing makefile s as a Makefile. It counting them as Other . So where I should have say 5 Makefile s and 10 Other files, its saying i have 0 makefiles and 15 Other files. c , h and o files work/count fine. Any help appreciated

You can use strlen() to determine the length of each string in argv. Knowing the length you should be able to peak at the last element in the array.

NOTE: I won't write the code for your homework assignments.

You can use string function strlen which calculates the length of string and to use this you have to include header <string.h> . And from that you can check the last index of every word of command line argument that which condition it satisfies , like

for(int i = 1; i< argc; i++){
        int l=strlen(argv[i]);
    if(argv[i][l-1]=='c')
        cCount++;
    else if(argv[i][l-1]=='h')
        cHeadCount++;
    else if(argv[i][l-1]=='o')
        objCount++;
    else if(argv[i][l-1]=='m')
        makeCount++;
    else othCount++;


   }

You can use switch statement to do this:

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

int main(int argc, char* argv[]){
        int cCount = 0;
        int cHeadCount = 0;
        int objCount = 0;
        int makeCount = 0;
        int othCount =0;
        int len = 0;

        for(int i = 1; i< argc; i++){
                len = strlen (argv[i]);
                /* Assuming that any argv (filename) which either 
                 * do not have any extension or extension of 
                 * more than 1 character goes under "other" file catagory
                 */
                if (len > 2 && (argv[i][len - 2] != '.')){
                        othCount++;
                        continue;
                }
                switch(argv[i][len - 1]){
                        case 'h':
                                cHeadCount++;
                        break;
                        case 'c':
                                cCount++;
                        break;
                        case 'o':
                                objCount++;
                        break;
                        case 'm':
                                makeCount++;
                        break;
                        default:
                                othCount++;
                        break;
                }
        }
        printf("C Source: %d\n", cCount);
        printf("C Header: %d\n", cHeadCount);
        printf("Object: %d\n", objCount);
        printf("Make: %d\n", makeCount);
        printf("Other: %d\n", othCount);
        return 0;
}

The output of the program:

$ ./fm main.c main.o sub.c sub.o
C Source: 2
C Header: 0
Object: 2
Make: 0
Other: 0

Use strrchr like this

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

int main(int argc, char* argv[]){
    int cCount, cHeadCount, objCount, makeCount, othCount;
    cCount = cHeadCount = objCount = makeCount = othCount = 0;

    for(int i = 1; i < argc; i++){
        char *filename = strrchr(argv[i], '/');
        filename = filename ? filename + 1 : argv[i];

        char *ext = strrchr(filename, '.');
        if(strcmp(filename, "Makefile") == 0 || strcmp(filename, "makefile") == 0){//or use strcasecmp ?
            ++makeCount;
        } else if(ext == NULL) {
            ++othCount;
        } else if(ext[1] == 'c' && ext[2] == 0){
            ++cCount;
        } else if(ext[1] == 'h' && ext[2] == 0){
            ++cHeadCount;
        } else if(ext[1] == 'o' && ext[2] == 0){//or strcmp(ext, ".o")==0
            ++objCount;
        } else {
            ++othCount;
        }
    }
    printf("C Source: %d\n", cCount);
    printf("C Header: %d\n", cHeadCount);
    printf("Object: %d\n", objCount);
    printf("Make: %d\n", makeCount);
    printf("Other: %d\n", othCount);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM