【问题标题】:Searching a char value inside char array in C [closed]在C中的char数组中搜索char值[关闭]
【发布时间】:2018-12-15 06:06:32
【问题描述】:

我正在课堂上学习 c 语言,我创建了一些代码来在其他数组中查找某个数字,但是当我尝试使用 char 值时,我的代码与使用 int 值的方式不同。

这是我的代码:

#include<stdio.h>

int main () {
    int  num, i, j;
    char a[99], ele;

    printf("Enter the Character element:");
    // get the length of array by the num value
    scanf("%d", &num);

    printf("Enter the values: \n");
    // a loop for getting values for our a[array] line by line
    for ( i = 0 ; i < num ; i++ ) {
        // get value by index i for array a index by index
        //printf("%d\t", (i+1));
        //if ( i + 1  == num ) {
        //    scanf("%c", &a[i]);
        //} else {
            scanf("%c", &a[i]);
        //}

    }

    printf("Enter the Character elements to be searched:");
    // get the value for ele, to use ele for searching inside our a[array]
    scanf("%c", &ele);

    // we need to set i to 0 for our while loop
    j = 0;
    // use the while loop to
    while ( j < num && ele != a[j]) {
        j++;
    }
    if ( j < num ) {
        printf("Character found at the location = %d\n\n\n", j + 1);
    } else {
        printf("Character Not Found!\n\n\n");
    }
    return 0;

}

我尝试修复了很多次,但每次都出现错误,所以上面的那个是有效的,但它在输入过程中会限制一些输入值。

【问题讨论】:

  • 请添加一个示例输入,您的代码不符合您的预期
  • 如果我在这一行的 %c 之后添加 \n (scanf("%c", &a[i]);) 输入将固定但在输入最后一个值后第二个 scanf 这个 ( scanf("%c", &ele);) 不起作用,程序将结束。
  • 在运行此程序时仔细考虑键盘上按下的每个键。这里不仅仅是字母数字字符,从输入您的号码num 时按 Enter 开始。 Read this answer.
  • 请花点时间了解scanf()的行为
  • 这里的要点是,有充分的理由,%c 指令与大多数其他 scanf 指令的不同之处不仅仅是对应变量的预期类型。

标签: c arrays search input char


【解决方案1】:

您的程序中有一个问题导致了令人惊讶的行为:

  • scanf("%d", &amp;num) 从标准输入中读取一个数字,但将用户键入的换行符留作待读取的下一个字符。
  • scanf("%c", &amp;a[i]); 的进一步调用会读取此待处理的换行符和num-1 来自用户的更多字符,其余的输入待处理...
  • 最后的 scanf("%c", &amp;ele); 读取标准输入中待处理的任何字节,无论是来自用户的字符还是待处理的换行符。

因此程序的行为就好像它没有执行最后一个scanf()

您应该使用fgets() 一次读取用户的一行输入,或者使用循环刷新待处理的换行符:

#include <stdio.h>

int main() {
    int num, i, c;
    char a[99], ele;

    printf("Enter the number of character:");
    // get the length of array by the num value
    if (scanf("%d", &num) != 1 || num < 0 || num > 99)
        return 1;

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    printf("Enter the characters on a single line: \n");
    // a loop for getting values for our a[array]
    for (i = 0; i < num; i++) {
        if (scanf("%c", &a[i]) != 1)
            return 1;
    }

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    printf("Enter the character to be searched: ");
    // get the value for ele, to use ele for searching inside our a[array]
    if (scanf("%c", &ele) != 1)
        return 1;

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    // use a for loop
    for (i = 0; i < num && ele != a[i]; i++) {
        i++;
    }
    if (i < num) {
        printf("Character %c found at the offset %d\n\n\n", ele, i);
    } else {
        printf("Character %c Not Found!\n\n\n", ele);
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    感谢WhozCraiguser3121023 的建议,scanf(" %c", &amp;a[i]);scanf(" %c", &amp;ele); 中的空格会产生一些不需要的输入,所以这是我的代码,它的工作方式很有魅力。 :))

    我只是在%c 之前添加空格,一切都很好。

    #include<stdio.h>
    
    int main () {
        int  num, i, j;
        char a[99], ele;
    
        printf("Enter the Character element:");
        // get the length of array by the num value
        scanf("%d", &num);
    
        printf("Enter the values: \n");
        // a loop for getting values for our a[array] line by line
        for ( i = 0 ; i < num ; i++ ) {
            // get value by index i for array a index by index
            printf("%d\t", (i+1));
            scanf(" %c", &a[i]);
    
    
        }
    
        printf("Enter the Character elements to be searched:");
        // get the value for ele, to use ele for searching inside our a[array]
        scanf(" %c", &ele);
    
        // we need to set i to 0 for our while loop
        j = 0;
        // use the while loop to
        while ( j < num && ele != a[j]) {
            j++;
        }
        if ( j < num ) {
            printf("Character found at the location = %d\n\n\n", j + 1);
        } else {
            printf("Character Not Found!\n\n\n");
        }
        return 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 2017-10-30
      • 2014-03-26
      • 1970-01-01
      相关资源
      最近更新 更多