Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我是否在使用'&&';不正确?_C_Operators - Fatal编程技术网

C 我是否在使用'&&';不正确?

C 我是否在使用'&&';不正确?,c,operators,C,Operators,我有一个问题,我必须从用户那里获取3个单词作为输入。我要如何处理这些输入: 在第一个单词中,所有元音都应替换为“$” 在第二个单词中,所有辅音都应替换为“#” 第三个单词应该转换成大写 以下是我尝试的代码: #include <stdio.h> #include <string.h> int main() { char first[20], second[20], third[20]; int i, j; char vowel[5] = { '

我有一个问题,我必须从用户那里获取3个单词作为输入。我要如何处理这些输入:

  • 在第一个单词中,所有元音都应替换为“$”
  • 在第二个单词中,所有辅音都应替换为“#”
  • 第三个单词应该转换成大写
  • 以下是我尝试的代码:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char first[20], second[20], third[20];
        int i, j;
    
        char vowel[5] = { 'a', 'e', 'i', 'o', 'u' };
    
        printf("Enter first word: ");
        scanf("%s", first);
    
        printf("Enter second word: ");
        scanf("%s", second);
    
        printf("Enter third word: ");
        scanf("%s", third);
    
        for (i = 0; i < strlen(first); i++) {
            for (j = 0; j < 5; j++) {
                if (first[i] == vowel[j])
                    first[i] = '$';
            }   
        }
        printf("Final strings are: \n");
        printf("%s", first);
    
        for (i = 0; i < strlen(second); i++) {
            if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'e');
               second[i] = '#';
        }
        printf("%s", second);
        printf("%s", strupr(third));
    }
    
    但预期产出是:

    Enter first word: kali
    Enter second word: kali
    Enter third word: kali
    Final strings are:
    k$l$#a#iKALI
    

    我做错了什么?

    您的代码有很多问题。如果启用编译器警告,您将看到以下内容:

    $ clang -Wall -Wextra -std=c11 -pedantic-errors b.c
    b.c:32:109: warning: if statement has empty body [-Wempty-body]
      ...!= 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'u');
                                                                            ^
    b.c:32:109: note: put the semicolon on a separate line to silence this warning
    b.c:37:18: warning: implicit declaration of function 'strupr' is invalid in C99
          [-Wimplicit-function-declaration]
        printf("%s", strupr(third));
                     ^
    b.c:37:18: warning: format specifies type 'char *' but the argument has type
          'int' [-Wformat]
        printf("%s", strupr(third));
                ~~   ^~~~~~~~~~~~~
                %d
    b.c:20:18: warning: comparison of integers of different signs: 'int' and
          'unsigned long' [-Wsign-compare]
        for(i = 0; i < strlen(first); i++){
                   ~ ^ ~~~~~~~~~~~~~
    b.c:31:18: warning: comparison of integers of different signs: 'int' and
          'unsigned long' [-Wsign-compare]
        for(i = 0; i < strlen(second); i++){
                   ~ ^ ~~~~~~~~~~~~~~
    5 warnings generated.
    /tmp/b-8f1874.o: In function `main':
    b.c:(.text+0x22a): undefined reference to `strupr'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
    $clang-Wall-Wextra-std=c11-学究式错误b.c
    b、 c:32:109:警告:if语句的正文为空[-Wempty body]
    ...!= '我和第二个[我]!='第二个[我]!='第二个[我]!='u');
    ^
    b、 c:32:109:注意:将分号放在另一行以使此警告静音
    b、 c:37:18:警告:C99中函数“strupr”的隐式声明无效
    [-Wimplicit函数声明]
    printf(“%s”,strupr(第三));
    ^
    b、 c:37:18:警告:格式指定类型“char*”,但参数的类型为
    “int”[-Wformat]
    printf(“%s”,strupr(第三));
    ~~   ^~~~~~~~~~~~~
    %d
    b、 c:20:18:警告:比较不同符号的整数:“int”和
    '无符号长'[-Wsign compare]
    对于(i=0;i
    如果你用谷歌搜索警告消息,你会得到关于如何处理它们的好提示。关于分号的警告使得
    If
    语句的正文为空,这就是问题的原因

    另一个问题是,您没有检查scanf的返回代码以查看读取是否成功。它将返回成功读取的次数


    避免使用
    strupr
    。这是一个不推荐使用的非标准函数。

    您在这句话中犯了一个愚蠢的错误:

        if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'u');
           second[i] = '#';
    
    您在
    if
    行的末尾添加了一个额外的
    ,使测试无效,下面的语句
    second[i]='#'
    无条件执行

    您应该在多行上打断如此长的表达式,避免重复测试,并使用
    {
    }

        if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && 
            second[i] != 'u') {
            second[i] = '#';
        }
    
    #包括
    #包括
    int main(){
    第一个字符[20],第二个字符[20],第三个字符[20];
    int i,j;
    字符元音[5]={'a','e','i','o','u'};
    printf(“输入第一个单词:”);
    scanf(“%s”,第一个);
    printf(“输入第二个单词:”);
    扫描频率(“%s”,秒);
    printf(“输入第三个单词:”);
    scanf(“%s”,第三);
    对于(i=0;i
    我认为你的问题在于你的
    if
    末尾有一个
    ,这会导致
    if
    结束。去掉分号,并用大括号将其下的部分括起来。除非你有充分的理由,否则千万不要忽略大括号。@Carcigenicate是的!这是唯一的问题。很好。不过认真地说,还是用
    {}
    。这不是问题所在,但它们可能会使问题变得更加明显,省略它们可能会在以后导致错误。@dave这远非唯一的问题,但正是它导致了您描述的错误。
    关于:
    如果(第二个[i]!=“a”!&second[i]!=“i”&second[i]!=“o”&second[i]!=“u”&second[i]!=“u”&second[i]!=“u”)
    我怀疑您是否想检查
    u
    两次,但是,代码确实需要修改以检查(有时是元音)
    y
    以增加问题:
    scanf(“%s”,第一);
    就像
    获取(第一)
    覆盖数组的可能性。最好使用
    scanf(“%19s”,第一个);
    -我的编译器发出警告of@Broman我真的不明白你没有检查scanf的返回码看读取是否成功,你能在回答中解释一下吗?@dave
        if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && 
            second[i] != 'u') {
            second[i] = '#';
        }
    
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    char first[20], second[20], third[20];
    int i, j;
    
    char vowel[5] = { 'a', 'e', 'i', 'o', 'u' };
    
    printf("Enter first word: ");
    scanf("%s", first);
    
    printf("Enter second word: ");
    scanf("%s", second);
    
    printf("Enter third word: ");
    scanf("%s", third);
    
    for (i = 0; i < strlen(first); i++) {
        for (j = 0; j < 5; j++) {
            if (first[i] == vowel[j])
                first[i] = '$';
        }   
    }
    printf("Final strings are: \n");
    printf("%s", first);
    
    for (i = 0; i < strlen(second); i++) {
        if (second[i] != 'a' && second[i] != 'e' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u') // You mistakenly put a semicolon here
           second[i] = '#';
    }
    printf("%s", second);
    printf("%s", strupr(third)); }