我使用'&&'不正确吗?

时间:2018-09-02 16:55:57

标签: c operators

我有一个问题,我必须从用户那里输入3个单词作为输入。我与该输入有何关系:

  1. 首先,所有元音应替换为'$'
  2. 第二句话,所有辅音都应替换为“#”
  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));
}

注意:所有3个单词应在输出屏幕上串联

输出:

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

但是预期的输出是:

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

我做错了什么?

3 个答案:

答案 0 :(得分:3)

您在此声明中犯了一个愚蠢的错误:

    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] = '#';
    }

答案 1 :(得分:2)

您的代码有很多问题。如果打开编译器警告,则会看到以下内容:

$ 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)

如果您搜索警告消息,则将获得有关如何处理这些警告的良好提示。有关导致if语句为空的分号的警告是导致您遇到问题的原因。

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

避免使用strupr。这是不推荐使用的非标准功能。

答案 2 :(得分:0)

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