我需要输入一个非常长的字符串,我不需要保留任何内存(不需要稍后读取字符串,只需逐个字符读取)。如何在不接收完整字符串的情况下处理字符串? 这是我的代码段:
for(length = n; length > 0; length--) {
input = getchar();
if (input == ...
...
显然这不起作用,但它使我的意图明确。我怎样才能实现我的目标?
答案 0 :(得分:1)
如果我弄错了,那么你可以这样试试:
while (1)
{
char c = (char)getchar();
if(c=='\n')
break;
// do your logic here
}
答案 1 :(得分:0)
以下逻辑将帮助您
for(length = n; length > 0; length--)
{
input = getchar();
if (input is required values)
{
process it
}
else if (input is end of input indicator)
{
break;
}
else //unwanted characters
{
continue; //skip it
}
}