在C - Argv中测试等值线到字符数组

时间:2018-02-19 20:47:45

标签: c pointers

我正在尝试创建一个简单的命令行测试,因为is / is不是isogram(一个单词中没有重复的字母),但是我在使用argv作为字符数组时遇到了问题。

我做了一些搜索,这个答案使用strcopy,然后导致我更详细malloc answer。可能是因为我糟糕的google-foo我找不到循环argv字符的例子。我试过的这段代码是切片字符串?:

 /* 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;
 }

输出:

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

注意:我尝试char *isogram[]=NULL认为这是我想要初始化的内容,但正如网站建议的那样,这只适用于char *isogram = NULL

编辑:我知道如何测试它们,我只是不能让每个角色相互比较。每个索引都返回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]);
     }
 }

2 个答案:

答案 0 :(得分:3)

您不需要任何argv[1]处理。您只需将其传递给检测功能。

code.c

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


int detect_isogram(const char *word) {
    size_t len = strlen(word), i = 0, j = 0;
    for (i = 0; i + 1 < len; i++)
        for (j = i + 1; j < len; j++)
            if (word[i] == word[j])
                return 0;
    return 1;
}

int detect_isogram2(const char *word) {
    size_t len = strlen(word), i = 0;
    for (i = 0; i + 1 < len; i++)
        if (strchr(word + i + 1, word[i]))
            return 0;
    return 1;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("error, bad arguments!\n");
        return 1;
    } else {
        printf("detect_isogram returned: %d\ndetect_isogram2 returned: %d\n", detect_isogram(argv[1]), detect_isogram2(argv[1]));
        return 0;
    }
}

备注

  • 算法很简单:对于字符串中的每个字符(从1 st 开始,除去最后一个),检查它是否在其余部分字符串
    • 支票可以是:
      • 手动(遍历字符串的剩余部分(从搜索后的字符 开始),并比较字符) - detect_isogram
      • 通过[cplusplus]: strchr - detect_isogram2

<强>输出

root@testserver:/home/cfati/work/so/q048873942# gcc code.c -o isogram
root@testserver:/home/cfati/work/so/q048873942# ./isogram testing
detect_isogram returned: 0
detect_isogram2 returned: 0
root@testserver:/home/cfati/work/so/q048873942# ./isogram ""
detect_isogram returned: 1
detect_isogram2 returned: 1
root@testserver:/home/cfati/work/so/q048873942# ./isogram 1
detect_isogram returned: 1
detect_isogram2 returned: 1
root@testserver:/home/cfati/work/so/q048873942# ./isogram 12345678
detect_isogram returned: 1
detect_isogram2 returned: 1
root@testserver:/home/cfati/work/so/q048873942# ./isogram 123456781
detect_isogram returned: 0
detect_isogram2 returned: 0
root@testserver:/home/cfati/work/so/q048873942# ./isogram qq
detect_isogram returned: 0
detect_isogram2 returned: 0


<强> @ EDIT0

  • 合并@ chux的改进建议

答案 1 :(得分:0)

如果你只是想强暴它

int main(void) {
    char str1[] = "helloworld";
    int repeat = 0;

    for (int i = 0; i < strlen(str1); i++) {
        for (int j = i + 1; j < strlen(str1); j++) {
            if (str1[i] == str1[j]) {
                repeat = 1;
                break;
            }
            if (repeat == 1) {
                break;
            }
        }
    }

    if (repeat == 1) {
        printf("There are repeat chars\n");
    }
    else {
        printf("There are no repeats\n");
    }
}

当然,你可以用你的case替换str1和argv [1]。需要注意的是,第二个循环总是在第一个循环当前所在的索引之后启动一个char。这可以防止您将相同的字符与自身进行比较。