如何检查命令行参数中的最后一个字符?

时间:2017-10-19 03:34:16

标签: c command-line

所以我试着计算命令行中的文件数量,这是我到目前为止所得到的

public class vidsav {

public vidsav(ApexPages.StandardController controller) {
    loadData();
}

public List<Account> studentsR {get;set;}
public String SelectedStudentId {get;set;}      

public void loadData() {

    studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];

}

public void deleteStudent(){

   studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];

    if(studentsR.size() > 0 || studentsR[0].Id != ''){

      delete studentsR;

    }
    loadData();

  }

}

因此,如果您键入# 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); } 之类的内容,则应该

$ ./fm main.c main.o sub.c sub.o

我需要帮助的是for循环中的C source: 2 C header: 0 Object: 2 Make: 0 Other: 0 语句。 BTW是for循环正确吗?是否有一个函数将返回字符串的最后一个字符?从我所看到的,我似乎不记得一个,但我可能是非常错的。

如果我发现这个错误或正确的话,请告诉我。任何帮助赞赏。

编辑:

继承人if循环中的内容:

for

现在的问题是它没有将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++; } } 识别为Makefile。它将它们计为makefile。所以我应该说5 Other和10 Makefile个文件,它说我有0 Other和15 makefiles个文件。 Otherch个文件可以正常运行。任何帮助表示赞赏

4 个答案:

答案 0 :(得分:0)

您可以使用strlen()来确定argv中每个字符串的长度。知道你应该能够在数组的最后一个元素达到峰值的长度。

注意:我不会为您的家庭作业编写代码。

答案 1 :(得分:0)

您可以使用字符串函数strlen来计算字符串的长度,并且要使用它,您必须包含标题<string.h>。从那里你可以检查命令行参数的每个单词的最后一个索引,它满足哪个条件,如

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++;


   }

答案 2 :(得分:0)

您可以使用switch语句执行此操作:

# 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;
}

该计划的输出:

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

答案 3 :(得分:0)

像这样使用strrchr

#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);
}