c - 我收到运行时错误,我无法获取两个字符值 a 和 b

标签 c

如果我为变量 s 提供输入“hello”,为变量提供 a 我无法提供输入

#include<stdio.h>
#include<string.h>
int main(){
    char s[100],a,b;
    //i am not able to get this value,please help me how to get three variables s,a,b at runtime
    scanf("%c",s);
    scanf("%c",a);
    scanf("%c",b);
    int n=strlen(s),count=0;
    for(int i=0;i<n;i++){
        if(s[i]==a && s[i+1]== b)
            count++;
    }
    printf("%d",count);
    return 0;
}

最佳答案

首先尝试使用 scanf("%c",&a) scanf 。 然后仅使用一个 scanf 读取三个变量。 试试这个程序,它将解决您的问题:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[100], a, b;
    //i am not able to get this value,please help me how to get three variables s,a,b at runtime
    scanf("%s %c %c", s, &a, &b);
    int n = strlen(s), count = 0;
    for(int i = 0; i < (n - 1); i++){
        if(s[i] == a && s[i+1] == b)
            count++;
    }
    printf("%d",count);
    return 0;
}

关于c - 我收到运行时错误,我无法获取两个字符值 a 和 b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44829972/

相关文章:

c - 用于在 c 中创建螺旋形状图案的嵌套循环

java - 无法写入核心转储。默认情况下,在 Windows 客户端版本上不启用小型转储,同时从 java 调用 dll

c - QEMU 在加载内核时崩溃

c++ - 是否跳过/删除 ELF 文件中的 `PHDR` 程序头以执行可执行文件?如果是这样,为什么?

c - 在 x86 汇编方面需要一些帮助

c++ - 指针问题

C - 嵌套 for 循环不打印多个元素

c - getchar 和 SIG34,实时事件 34

c - LED segmentation 市场的值(value)不断下降

c++ - 'a' == 'b' 。这是个好办法吗?