質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

3回答

1159閲覧

ファイルから文字を取り出し、出現頻度をカウント

Reizo

総合スコア13

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2017/12/28 03:12

編集2017/12/28 12:34

Cを用いてASCIIで書かれているファイルからアルファベットとスペース区切り、改行のみの文字列を読み込み、文字の組にして取り出し、出現頻度をカウントしたいと思っています。

取り出した文字の組とその出現回数をファイルに出力したのですが、一部文字化けを起こしてしまいます。

ご指摘をお願いします。

usethisfile.txt

end of the project gutenberg ebook of the lincoln country in pictures by carl frazier and rosalie frazier end of this project gutenberg ebook the lincoln country in pictures this file should be named h htm or h zip this and all associated files of various formats will be found in http www gutenberg org produced by stephen hutcheson and the online distributed proofreading team at http www pgdp net updated editions will replace the previous one the old editions will be renamed creating the works from print editions not protected by u s copyright law means that no one owns a united states copyright in these works so the foundation and you can copy and distribute it in the united states without permission and without paying copyright royalties special rules set forth in the general terms of use part of this license apply to copying and distributing project gutenberg tm electronic works to protect the project gutenberg tm concept and trademark project gutenberg is a registered trademark and may not be used if you charge for the ebooks unless you receive specific permission if you do not charge anything for copies of this ebook complying with the rules is very easy you may use this ebook for nearly any purpose such as creation of derivative works reports performances and research they may be modified and printed and given away you may do practically anything in the united states with ebooks not protected by u s copyright law redistribution is subject to the trademark license especially commercial redistribution start full license the full project gutenberg license please read this before you distribute or use this work to protect the project gutenberg tm mission of promoting the free distribution of electronic works by using or distributing this work or any other work associated in any way with the phrase project gutenberg you agree to comply with all the terms of the full project gutenberg tm license available with this file or online at www gutenberg org license section general terms of use and redistributing project gutenberg tm electronic works a by reading or using any part of this project gutenberg tm electronic work you indicate that you have read understand agree to and accept all the terms of this license and intellectual property trademark copyright agreement if you do not agree to abide by all the terms of this agreement you must cease using and return or destroy all copies of project gutenberg tm electronic works in your possession if you paid a fee for obtaining a copy of or access to a project gutenberg tm electronic work and you do not agree to be bound by the terms of this agreement you may obtain a refund from the person or entity to whom you paid the fee as set forth in paragraph

main.c

 1 #include<stdio.h> 2 3 int main(void){ 4 5 FILE *fp; 6 int three[17576][4]= {{0}}, i=0, j=0; 7 char word[256]={}; 8 9 fp = fopen("usethisfile.txt", "r"); 10 11 12 while(fgets(word, 256, fp) != NULL){ 13 while(word[i] != 0x00){ 14 do{ 15 if(((three[j][0]==word[i]) && (three[j][1]==word[i+1])) && (three[j][2]==word[i+2])){ 16 three[j][3]++; 17 break; 18 } 19 else if(three[j][0]==0){ 20 three[j][0] = word[i]; 21 three[j][1] = word[i+1]; 22 three[j][2] = word[i+2]; 23 three[j][3]++; 24 break; 25 } 26 }while(three[j++][0] != 0); 27 j=0; 28 i++; 29 } 30 i=0; 31 32 } 33 34 fclose(fp); 35 fp = fopen("filethree.txt", "w"); 36 37 i=0; 38 while(three[i][0] != 0){ 39 fprintf(fp, "%c%c%c %d\n", three[i][0], three[i][1], three[i][2], three[i][3]); 40 i++; 41 } 42 fclose(fp); 43 44 return 0; 45 }

ファイル出力結果

end 2 nd 22 d o 2 of 17 of 18 f t 10 th 41 the 29 he 24 e p 6 pr 21 pro 21 roj 13 oje 13 jec 14 ect 26 ct 13 t g 10 gu 12 gut 15 ute 18 ten 15 enb 15 nbe 15 ber 15 erg 15 rg 17 g e 2 eb 5 ebo 6 boo 6 ook 6 ok 3 k o 1 e l 2 li 9 lin 4 inc 2 nco 2 col 2 oln 2 ln 2 n c 3 co 15 cou 2 oun 5 unt 2 ntr 2 try 2 ry 3 y i 2 in 12 in 11 n p 3 pi 2 pic 2 ict 2 ctu 3 tur 3 ure 2 res 3 es 12 s 9 b 3 by 7 by 8 y  1 \00 11 \00\00 15 ...

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

int count[5000]={0};

ぜんぜん足りんでしょ。英小文字だけでも 26^3 = 17576通りよ?

てか、意味のある変数名を使いなさい。tてなんですか。
そしてちゃんとコメント書きなさい。

投稿2017/12/28 03:26

編集2017/12/28 03:28
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

一部文字化けを起こしてしまいます。

起こすでしょうね。

while(word[i] != 0x00){ do{ if(((three[j][0]==word[i]) && (three[j][1]==word[i+1])) && (three[j][2]==word[i+2])){

これだと word[i+1], word[i+2] が 0x00 となることがあり、文字列の終端を超えてしまいます。

投稿2017/12/28 04:27

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Reizo

2017/12/28 09:30 編集

main.c19行目のif文内にアルファベットと空白以外の文字を弾く条件分岐を追加したことで解決できました。 ご指摘頂きありがとうございました。 if((isalpha(word[i])==0)&&(word[i]!=" ")){   break; } if((isalpha(word[i+1])==0)&&(word[i+1]!=" ")){   break; } if((isalpha(word[i+2])==0)&&(word[i+2]!=" ")){   break; }
guest

0

取り出した文字の組とその出現回数をファイルに出力したのですが、ファイルに書き込めず困っています。

症状を適切に説明しなさい。

  • 書き込みファイルがopenできない
  • ナカミからっぽである
  • 期待した結果が書かれていない

などなど。

※ 「具合が悪い」の一言で薬をくれる医者はいません。

投稿2017/12/28 03:16

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問