我写了一个关于字符串输入和在字符串中查找元音的代码。除了程序没有退出do while循环外,一切正常。
do{
i = 0; j = 1;
str = NULL;
input = '\0';
for(l = 0; l < 5; l++ ) { /* initializing arrays with zeros */
lowercase_vowel[l] = 0;
uppercase_vowel[l] = 0;
}
str = (char*)malloc(sizeof(char)); /* allocating space for a character in str */
printf("\nEnter String : ");
while(input != '\n' ) /* until user press enter */
{
input = getc(stdin); /* taking the input */
str = (char*)realloc(str,j*sizeof(char)); /* re-allocate memory for character read to be stored */
str[i] = input; /* store read character */
i++;
j++;
}
str[i]='\0'; /* appending null character to the string */
/*Some calculations here */
printf("\nDo you want to continure? (Y / N)");
scanf("%c", &option);
}while( (option != 'N') || (option != 'n') );
问题是,当我提供选项时,循环不会结束,而且它也会跳过部分
printf("\nEnter String : ");
并根据选项中的值进行计算。如果有人帮我解决这个问题,我真的很感激
答案 0 :(得分:1)
条件
(option != 'N') || (option != 'n')
对于option
的任何给定值始终为true。您需要将||
(或)替换为&&
(和)。