#include<stdio.h>#define M 20int getline(char line[ ]);????????//獲得字符串void cpystr(char longest[ ], char line[ ]);????????????//將最長的字符串復(fù)制到 longest[ ]中int main(){??char line[M],longest[M];?int len;?int max=0;?while((len=getline(line))>0)??????? // this statement test if there is another line?{??if(len>max)??{????max=len;???cpystr(longest,line);??}???}?if(max>0)??printf("%d\t%s\n",max,longest);? //line[] and longest must be the char type here ?else printf("please input a line\n");?// in case of error here?return 0;??????? }int getline(char line[]){??int i;?int c;?for(i=0;i<M-i && ((c=getchar())!=EOF) && c!= '\n'; i++)?{??line[i]=c;?}?line[i]='\0';??????// this statement can not be left out, it's the symbol of end ?return i-1;}void cpystr(char longest[],char line[]){?int i=0;?while((longest[i]=line[i])!='\0')??i++;?longest[i]='\0';}這個程序是檢測輸入的字符串,并輸出最長的一個字符串及其長度;在程序中有兩處加粗的地方,是給字符串作為結(jié)尾。我的問題是為什么上面那個line [i] ='\0'去掉后后影響結(jié)果,而下面那個longest [i] = '\0' 卻不已要?
1 回答

Atlas_Wu
TA貢獻(xiàn)3條經(jīng)驗(yàn) 獲得超3個贊
自己想到了原因,就自己回答一下。
while((longest[i]=line[i])=!"\0")
是先把line[i]的值賦予longest[i],然后才判斷是否等于“\0”,因此line[i]最后的“\0”已經(jīng)賦予給了longest[i],
不需要再給最后的longest[i]賦予"\0"
- 1 回答
- 0 關(guān)注
- 1424 瀏覽
添加回答
舉報
0/150
提交
取消