当我在输入“a”或“b”后尝试在 if 函数内部运行 scanf 时,它会立即运行并退出程序而不获取输入。有没有办法修复它,以便 scanf 在 if 和 else if 函数内部工作?
#include <stdio.h>
#include <string.h>
int top=-1;
int word;
char stack_string[100];
void push(char word);
char pop(void);
int main()
{
char input;
char str[100];
int i;
printf("press [a] for palindrome check, [b] for string reversal:");
scanf("%c", &input);
if (input == 'b'){
printf("Input a string: ");
scanf("%[^\n]s", str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed String is: %s\n",str);
}
else if(input == 'a'){
char str[100];
int count = 0;
printf("Input a string: ");
scanf("%[^\n]s", str);
for (i = 0; i < strlen(str); i++)
{
push(str[i]);
}
for (i = 0; i < strlen(str); i++)
{
if (str[i]==pop())
count++;
}
if (count == strlen(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
}
return 0;
}
void push(char word)
{
top=top+1;
stack_string[top]=word;
}
char pop()
{
word = stack_string[top];
top=top-1;
return word;
}
最佳答案
关于c - 我的 scanf 函数在 if 函数内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752219/