代码之家  ›  专栏  ›  技术社区  ›  Wilson

基于命令行参数将字符串合并在一起。

  •  0
  • Wilson  · 技术社区  · 7 年前

    我试图用C语言编写一个程序,根据给定的命令行参数,以两种不同的方式将2个字符串合并在一起。

    “-i”交替合并字符串,如下所示

    combine_字符串i 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 组合管柱为:a1b2c3d4e567

    “-w”将两个字符串连接在一起,并在字符串的每个字符之间插入一个“*”,如下所示

    combine_字符串-w 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 组合字符串为:a b c d e*1*2*3*4*5*6*7

    这就是我目前得到的。

    #include<stdio.h>
    #include<string.h>
    #include<memory.h>
    
    void alt_merge(char *string1,char *string2,char *stringfinal){
    
    while (*string1 != '\0' && *string2 != '\0')
    {
        *stringfinal++= *string1++;
    *stringfinal++ = *string2++;
    }
    while (*string1 != '\0')
        *stringfinal++=*string1++;
    while (*string2 != '\0')
        *stringfinal++ = *string2++;
    *stringfinal='\0';
    }
    
    void concate_star(char *string1,char *string2,char *stringfinal){
    
    strcpy(stringfinal,strcat(string1,string2));
    }
    
    int main(int argc,char *argv[]){
    char *string1[30+1];
    char *string2[30+1];
    char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
    
    if(argv[1]=='-i'){
            printf("Please enter a string of maximum 30 characters: ");
            scanf("%s",&string1);
            printf("Please enter a string of maximum 30 characters: ");
            scanf("%s",&string2);
    
            alt_merge(string1,string2,stringfinal);
    
            printf("The combined string is: %s",stringfinal);
    }      
    else if(argv[1]=='-w'){
            printf("Please enter a string of maximum 30 characters: ");
            scanf("%s",&string1);
            printf("Please enter a string of maximum 30 characters: ");
            scanf("%s",&string2);
    
            concate_star(*string1,*string2,*stringfinal);
    
            printf("The combined string is: %s",stringfinal);
    }       
    
    return 0;
    }
    

    我收到这些错误消息。

    jdoodle.c: In function 'main':
    jdoodle.c:27:23: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
         char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
                           ^~~~~~
    jdoodle.c:27:23: warning: incompatible implicit declaration of built-in function 'malloc'
    jdoodle.c:27:23: note: include '<stdlib.h>' or provide a declaration of 'malloc'
    jdoodle.c:27:37: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
         char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
                                         ^~~~~~~
    In file included from jdoodle.c:2:0:
    /usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
     extern size_t strlen (const char *__s)
                   ^~~~~~
    jdoodle.c:27:53: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
         char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
                                                         ^~~~~~~
    In file included from jdoodle.c:2:0:
    /usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
     extern size_t strlen (const char *__s)
                   ^~~~~~
    jdoodle.c:29:17: warning: multi-character character constant [-Wmultichar]
         if(argv[1]=='-i'){
                     ^~~~
    jdoodle.c:29:15: warning: comparison between pointer and integer
         if(argv[1]=='-i'){
                   ^~
    jdoodle.c:35:23: warning: passing argument 1 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
                 alt_merge(string1,string2,stringfinal);
                           ^~~~~~~
    jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
     void alt_merge(char *string1,char *string2,char *stringfinal){
          ^~~~~~~~~
    jdoodle.c:35:31: warning: passing argument 2 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
                 alt_merge(string1,string2,stringfinal);
                                   ^~~~~~~
    jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
     void alt_merge(char *string1,char *string2,char *stringfinal){
          ^~~~~~~~~
    jdoodle.c:39:22: warning: multi-character character constant [-Wmultichar]
         else if(argv[1]=='-w'){
                          ^~~~
    jdoodle.c:39:20: warning: comparison between pointer and integer
         else if(argv[1]=='-w'){
                        ^~
    jdoodle.c:45:44: warning: passing argument 3 of 'concate_star' makes pointer from integer without a cast [-Wint-conversion]
                 concate_star(*string1,*string2,*stringfinal);
                                                ^
    jdoodle.c:19:6: note: expected 'char *' but argument is of type 'char'
     void concate_star(char *string1,char *string2,char *stringfinal){
          ^~~~~~~~~~~~
    

    我是编程新手,所以非常感谢您的帮助和建议。

    1 回复  |  直到 7 年前
        1
  •  0
  •   user2736738    7 年前

    两个问题,你没有包括 stdlib.h 其中有函数的声明 malloc() .

    此外,您将错误的参数传递给 strlen() 但你通过了 char** . 其他功能也一样。您的参数不匹配。

    char *string1[30+1];
    char *string2[30+1];
    

    你想要的

    char string1[30+1];
    char string2[30+1];
    

    你也应该使用 strcmp 当你比较时 "-i" 具有 argv[1] .

    现在 argv 基本上是数组 char* “-我” . (不是 '-i' ).

    还有一件有趣的事,

    编译器说

    jdoodle.c:29:15: warning: comparison between pointer and integer
         if(argv[1]=='-i'){
                   ^~
    

    包含多个c字符的普通字符文字是 多字符文字。多字符文字的类型为int和 实现密集值。

    “-我” 是多字符文字。此具有类型 int 这就是为什么要将 pointer 整数 .

    还有一件事你应该清楚地知道,当你把一个数组传递给一个函数时,它会衰减成指向数组第一个元素的指针。

    char *s[100] 衰变为 因为数组的内容是 字符* 指向它的指针是 字符** .