开发者社区> 问答> 正文

初学c,squeeze(s1,s2)练习出错,请指导

squeeze(s1, s2),将字符串s1中任何以字符串s2中字符匹配的字符都删掉

#include <stdio.h>

void squeeze(char s1[], char s2[])
{
            int i, j, k;

        for (i = k = 0; s1[i] != '\0'; i++){
                for (j = 0; s2[j] != '\0' && s2[j] != s1[j]; j++)
                        ;
                if (s2[j] == '\0')
                        s1[k++] = s1[i];
        }
        s1[k] = '\0';
}

    main()
    {
            char s1[], s2[];
            s1 = "1234567";
            s2 = "345";
            squeeze(s1, s2);
            printf ("%s, %s" s1, s2);
    }
出错显示

squeeze_12.c: In function ‘main’:
squeeze_12.c:18:7: error: array size missing in ‘s1’
squeeze_12.c:18:13: error: array size missing in ‘s2’
squeeze_12.c:19:5: error: incompatible types when assigning to type ‘char[1]’ from type ‘char *’
squeeze_12.c:20:5: error: incompatible types when assigning to type ‘char[1]’ from type ‘char *’
squeeze_12.c:22:19: error: expected ‘)’ before ‘s1’
squeeze_12.c:22:19: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat]
squeeze_12.c:22:19: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat]

展开
收起
a123456678 2016-06-06 11:47:25 2465 0
1 条回答
写回答
取消 提交回答
  • #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE  1
    #define FALSE 0
    
    void squeeze(char s1[], char s2[]);
    
    int main(void)
    {
        char s1[] = "abcdef";
        char s2[] = "bdf";
        squeeze(s1, s2);
        printf("%s\n", s1);
    
        return EXIT_SUCCESS;
    }
    
    /* squeeze:  delete all characters from s1 that match any in s2 */
    void squeeze(char s1[], char s2[])
    {
        int i, j, k;
        int instr2;
    
        for (i = j = 0; s1[i] != '\0'; i++) {
            instr2 = FALSE;
            for (k = 0; s2[k] != '\0' && !instr2; k++)
                if (s2[k] == s1[i])
                    instr2 = TRUE;
            if (!instr2)
                s1[j++] = s1[i];
        }
        s1[j] = '\0';
    }
    2019-07-17 19:28:07
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载