main 함수를 살펴보면 오류는 첫 번째 입력에 있습니다. \ q를 입력하면 예상대로 프로그램이 종료되지 않습니다.왜 프로그램이 종료되지 않는지 확실하지 않음
왜 이런 일이 일어날 지 설명 할 수 있습니까?
fgets를 scanf로 변경하면 \ q 부분이 정상적으로 작동하지만 (scanf를 사용하면) 공백이있는 문자열을 입력하면 공백 다음에 입력이 종료됩니다.
#include <stdio.h>
#include <cstring>
#include <ctype.h>
int count(char *input, char c) {
int k = strlen(input);
return 0;
}
void subanagram() {
char yes[50] = "yes";
char no [50] = "no";
char play[100];
char sa[100];
char strin[100];
char quitt[75] = { '/', 'q', '\0'};
do {
printf("Would you like to play the sub-anagram game?");
//scanf("%s", play);
fgets(play, 100, stdin);
if(stricmp(play, yes) == 0) {
printf("Enter a potential sub-anagram (or /q to quit): ");
scanf("%s", sa);
if(stricmp(sa, quitt) == 0) {
break;
}
}
}
while (stricmp(yes, play) == 0 || stricmp(no, play) == 0);
}
void main() {
char string[100];
char pally[75];
char nospace[100];
char reversed[100];
char yes[75] = {'y', 'e', 's', '\0'};
char quit[75] = { '\\', 'q', '\0'};
char no[75] = {'n', 'o', '\0'};
char play[50];
int p, i, k, j=0;
printf("Enter a word or phrase (or \\q to quit): ");
fgets(string, 100, stdin);
//scanf("%s", string);
k = strlen(string);
if(stricmp(quit, string) == 0) {
printf("Thank you for using my program!\n");
return;
}
do {
printf("Would you like to see if the phrase is a palindrome?");
scanf("%s", pally);
} while (stricmp(yes, pally) == 1 || stricmp(no, pally) == 1);
if(stricmp(yes, pally) == 0) {
for(i=0; i<k; i++) {
if(!isspace(string[i]) == 1) {
nospace[j] = string[i];
nospace[j+1] = '\0';
j++;
}
}
strcpy(reversed, nospace);
strrev(reversed);
if(stricmp(nospace, reversed) == 0) {
printf("The phrase %s IS a palindrome.\n", string);
subanagram();
}
else { printf("The phrase %s IS NOT a palindrome.\n", string);
subanagram();
}
}
if(stricmp(pally, no) == 0) {
/* printf("Would you like to play the sub-anagram game?");
scanf("%s", play);
if(strcmp(play, yes) == 0) {
subanagram();
}*/
subanagram();
}
}
btw가 너무 커서 코드를 들여 보내십시오. 그러면 읽을 수 있고 해당 블록을 볼 수 있습니다! –