gpt4 book ai didi

c - C语言中的字符串输入

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

同时,每当我使用内置函数 getsscanf 函数依次获取输入字符串和字符时,我都会遇到问题。

首先,每当我使用 gets 获取输入字符串,然后借助 scanf 函数获取输入字符时,编译器不会响应获取字符尽管它从 gets 函数获取输入并直接跳过 scanf 函数从用户处获取字符。

请参阅下面的代码并将其运行到您的编译器以了解问题

   #include <stdio.h>
#include <string.h>
int main()
{

int i,l,T,exit;

char str[1000];
char ch;

scanf("%d",&T);

while( (T-- ) ){


gets(str);

scanf("%c",&ch);

l = strlen(str);

exit = 0;

for( i = 0; i < l; i++ ){

if( str[i] == ch )
exit++;
}

if(!(exit))
printf("'\%c\' is not present\n",ch);

else
printf("Occurrence of '\%c\' in '\%s\' = %d\n",ch,exit);

}

return 0;

}

最佳答案

不要混合使用输入法。阅读此讨论以了解详细信息。

http://c-faq.com/stdio/gets_flush2.html

我建议用 scanf 替换 gets。

关于c - C语言中的字符串输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883551/

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