- 【2018年4月開学】東京通信大学Part.3
345 :名無し生涯学習[sage]:2018/11/22(木) 06:42:21.36 ID:2U8RyLV30 - >>340のほうが近いかな。
for(i=1;fgets(line,LINESIZE,stdin)!=NULL,i++;){ 1行を読み込む度にiに1を加算していく →つまりiに行番号が格納される for(ch=0; line[ch]!='\n';ch++); 1文字読み込む度にchに1を加算する。 改行が出てくるまで繰り返す。 つまりchに行内の文字数が格納される。 これは前のforの{}の中にあるので1行読み込む度に毎行行われる。 ここまで良い。 printf("%3d> ", (int)strlen(line)-1); せっかくカウントしたiとchを出力しようとしていない。 そこを>>341で改善している。 printf("%3d> ", i); printf("%[]d> ", ch); しかしこれだと 1>%[]2d Oh where, oh where has my little dog gone? 2>%[]2d Oh where, oh where can he be? 3>%[]2d With his ears cut short and his tail cut long, 4>%[]2d Oh where, oh where is he? このような出力になってしまう。 1[42] Oh where, oh where has my little dog gone? 2[30] Oh where, oh where can he be? 3[46] With his ears cut short and his tail cut long, 4[26] Oh where, oh where is he? こうなるように、不要な文字を消して、カッコの位置が正しくなるように修正する。 %2dの間に余計な文字はいれてはいけない。これは3文字連なったセットで意味を持つから。
|