C - 为什么这个删除所有评论的程序不起作用?



这是我的21次试用,我真的找不到应该在哪里搜索,下面的代码是删除单个评论行我正在使用缓冲区来收集所有数据,然后我尝试提交它们,但我怀疑在 if 语句中在线

if(arr[pos] == '/' &&  arr[pos+1] == '/')

但直到现在我都找不到区别 这是整个代码

#define         MAXSIZE         200
#define         ON              1
#define         OFF             0
char uncomentedBuffer[MAXSIZE];
char Buffer[MAXSIZE];
char comment[MAXSIZE];
void uncommen(char *arr);
//int getLine(char *arr, int lim);
int main(void)
{
int c, pos = 0;
while((c = getchar()) != EOF)
{
Buffer[pos] = c;
pos++;
}
uncommen(Buffer);
printf("%s",uncomentedBuffer);
return 0;
}
void uncommen(char *arr)
{
int pos = 0 ,nBS = 0, nAS = 0 ,scomment = OFF, bcomment = OFF;
while(pos <= MAXSIZE )
{
if(arr[pos] == '/' &&  arr[pos+1] == '/')
{
scomment = ON;
while(scomment == ON && arr[pos] != 'n')
{
pos++;
}
scomment = OFF;
}
uncomentedBuffer[pos] = arr[pos];
pos++;
}
}

我需要帮助

你的未编码字符串用一堆''(nul字符(初始化,这表示字符串的结束。您忽略了输入字符串的注释位置,并留下了一堆''

一种解决方案是创建一个特定的计数器,以避免在未编码的字符串中留下垃圾:

void uncommen(char *arr) 
{
int pos = 0, auxPos = 0 ,nBS = 0, nAS = 0 ,scomment = OFF, bcomment = OFF;
while(pos <= MAXSIZE )
{
if(arr[pos] == '/' &&  arr[pos+1] == '/')
{
scomment = ON;
while(scomment == ON && arr[pos] != 'n')
{
pos++;
}
scomment = OFF;
}
uncomentedBuffer[auxPos] = arr[pos];
pos++;
auxPos++;
}
}

相关内容

最新更新