[英]Do-while or while loop that will keep taking an input from user but will terminate after I input the string "exit" (case insensitive) in c language
[英]How do I make a while loop that will keep taking string input until i input nothing?
我在 C 工作,我想做一个循环,让我的代码不断请求输入,直到我输入“”?
char inputarray[1000];
int size;
printf("\nEnter a string: ");
fgets(inputarray,1000,stdin);
size = strlen(inputarray);
printf("%d \n", size);
printf("%s \n", inputarray);
我猜你正在寻找这样的东西:
#include <stdio.h>
#include <string.h>
int main(void)
{
char inputarray[1000];
int size;
do
{
printf("\nEnter a string: ");
if(!fgets(inputarray,1000,stdin)) /* Check for EOF , or NULL upon return */
{
return -1;
}
size = strlen(inputarray)-1; /* Ignoring '\n' */
printf("%d \n", size);
printf("%s ", inputarray); /* No need for '\n' since
fgets stores every character including newline in the buffer */
}while(strcmp(inputarray,"\n")); /* Meaning strcmp doesnt return 0 */
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.