我是否错误地使用了“&&”?

Am I using '&&' incorrectly?

我有一个问题,我必须从用户那里获取 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

我做错了什么?

你的代码有很多问题。如果您打开编译器警告,您将看到:

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

如果您 google 警告消息,您将获得有关如何处理它们的良好提示。关于使 if 语句具有空主体的分号的警告是导致您出现问题的原因。

另一个问题是你没有检查scanf的return代码来查看读取是否成功。它将return成功读取的次数。

避免使用 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] = '#';
    }
#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)); }