gpt4 book ai didi

字符串中的字符搜索

转载 作者:行者123 更新时间:2023-11-30 19:51:51 26 4
gpt4 key购买 nike

任何人都可以帮我修改我的代码吗

int main(void){

int ctr,wordLength;

char theWord[99];

ctr = 0;

printf("Enter a Word: ");
scanf("%s", & );
printf("Enter the letter you want to find: ");
scanf("%s", & );

while(ctr < wordLength | theWord[ctr]=='a'){

ctr++;
}

//output
}

期望输出

输入一个词:你好

输入您要查找的字母:z

在单词中找不到字母 z。

最佳答案

  • 你也可以这样做,

    #include <stdio.h>
    int main(void)
    {
    int ctr;
    char theWord[99], ch;
    ctr = 0;
    printf("Enter a Word: ");
    scanf("%s", theWord);
    printf("Enter the letter you want to find: ");
    scanf(" %c", &ch );

    for(int i = 0; theWord[i]; i++)
    {
    if(theWord[i] == ch)
    ctr++;
    }
    if(ctr != 0)
    printf("word is found\n");
    else
    printf("word is not found\n");
    }
  • 是的,我们也可以使用 while 循环来做到这一点,

    int i = 0;
    while(theWord[i] != '\0' )
    {
    if(theWord[i] == ch)
    ctr++;
    i++;
    }
    if(ctr != 0)
    printf("word is found\n");
    else
    printf("word is not found\n");

关于字符串中的字符搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com