2回目の投稿になります。前回、「英文が書かれたファイルを読み込んで,ファイルに含まれる文の数を数える」
(文の数を判定する文字は'.','?','!'の3つ)c言語のプログラムについて、2行以上のファイルの処理が上手くいかないということで質問させてもらいました。
今回は、!と?が複数並ぶことを想定して(!!や!?!?)文の数を数えるプログラムについて考えています。下にコードとテキスト2種類のせました。おそらくwhile(text[j+i]=='!'||text[j+1]=='?')以降の部分で間違えていることはわかるんですが、どう修正すればいいかわかりません。アドバイスしていただけるとありがたいです。
c言語 #include <stdio.h> #include <stdlib.h> #include<string.h> int main(void){ FILE *fp; char text_name[81], text[81]; int i=0,j=0, count=0; scanf("%s", text_name); fp=fopen(text_name, "r"); while(fgets(text, 81, fp)!=NULL){ while(text[i]!='\n'){ if(text[i]=='.'){ count++; } j=i; if(text[i]=='?'||text[i]=='!'){ while(text[j]=='!'||text[j]=='?'){ if(text[j]=='\0'){ count++; break; } j++; } count++; break; } /* if(text[i]=='.' || text[i]=='?' || text[i]=='!'){ count++; }*/ i++; } i=0; } printf("Number of sentences = %d\n", count); fclose(fp); return 0; }
sentences04.txt On the internet, people often use lots of extra punctuation marks. Here are some extracts from Twitter. Last night I was at like 860 followers and now I'm at 940???!!! Omg I'm freaking out. REALLY? REALLY?! The girls will perform on children in need on Friday and a morning show on Sunday. can they do a singing on Saturday in London????? New announcement tomorrow!!!! Show us some support??
sentences05.txt WOW!!!!!!!!!!!!!!!!!!!!!! Great!!!!!!!!!!!!!! My program works on all 5 examples???!!!?!?!?!?????? I can get a grade for this class!!! Yatta!!!! Yatta!!!! Yatta!!!!
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/26 02:37