C 不确定程序为什么不';不要终止

C 不确定程序为什么不';不要终止,c,string,loops,C,String,Loops,如果您向下看主函数,错误在于第一个输入。 当我键入\q时,我的程序不会像我希望的那样终止 有人能解释一下为什么会发生这种情况吗 我注意到,如果我将fgets更改为scanf,那么\q部分工作正常,但是(使用scanf)如果我输入带有任何空格的字符串,则输入将在空格后终止 #include <stdio.h> #include <cstring> #include <ctype.h> int count(char *input, char c) {

如果您向下看主函数,错误在于第一个输入。 当我键入\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();  
    } 
}
#包括
#包括
#包括
整数计数(字符*输入,字符c){
int k=strlen(输入);
返回0;
}
void subanagram(){
字符是[50]=“是”;
字符编号[50]=“否”;
字符播放[100];
char-sa[100];
charstrin[100];
char quitt[75]={'/','q','\0'};
做{
printf(“你想玩子字谜游戏吗?”);
//scanf(“%s”,play);
fgets(播放,100,标准播放);
如果(stricmp(播放,是)=0){
printf(“输入可能的子字谜(或/q退出):”;
scanf(“%s”,sa);
if(stricmp(sa,quit)==0){
打破
}
}
}
while(stricmp(yes,play)==0 | | stricmp(no,play)==0);
}
void main(){
字符串[100];
查尔·帕利[75];
char-nospace[100];
字符反转[100];
char yes[75]={'y','e','s','\0'};
char quit[75]={'\\','q','\0'};
字符编号[75]={'n','o','\0'};
角色扮演[50];
int p,i,k,j=0;
printf(“输入单词或短语(或\\q退出):”;
fgets(字符串,100,标准输入);
//scanf(“%s”,字符串);
k=strlen(字符串);
if(stricmp(退出,字符串)==0){
printf(“感谢您使用我的程序!\n”);
回来
}
做{
printf(“你想看看这个短语是否是回文吗?”);
scanf(“%s”,pally);
}而(stricmp(yes,pally)==1 | | stricmp(no,pally)==1);
如果(stricmp(是,pally)==0){

对于(i=0;ihmm),在第一次查看这个(太多)大型程序时,我的符号检测器在subanagram()中看到:

主要是:

char quit[75] = { '\\', 'q',
你的问题是:

"When I type \q, my program does not"
'\n'
(字符为换行符)作为您输入的字符串将包含在
fgets(stinrg,100,stdin)
的情况下,这与
scanf(“%s”,string)
不同。因此,退出字符串更改为
char quit[]=“\\q\n”
或者您删除了最后一个
\n'
,确定它必须是一种治疗,如您愿意。

请缩进您的代码(顺便说一句,代码太大了),以便可读,并且可以看到相应的块!
"When I type \q, my program does not"