strstrについて

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
yossiy

strstrについて

#1

投稿記事 by yossiy » 18年前

strstrについての課題です。
strstr関数について調べてはみたのですが、数を数えるのをどうやって作ればよいか見当もつきません。
説明もお願いします。



次のプログラムの英文の文章の中に冠詞の[the]の数をstrstr関数を使って数えるプログラムを作成する。
thenなどは数えないようにする。



#include <stdio.h>
#include <string.h>
int main(void)
{
int i,len;
char *found;
char a[/url]="Chiba University was founded in 1949, unifying several regional former national colleges and schools such as Chiba Medical College and Chiba Normal School. Its fundamental mission since then has been, as encapsulated by the inscription on the University Bell, ad altiora semper (always toward the higher), to equip students with the ability to make mature and informed judgments while nurturing and guiding their creativity. Pursuing these goals of excellence has resulted in Chiba University becoming one of the leading academic research centers of Japan.";

len = strlen(a);
for(i=0;i<len;i++){
found = strstr(a+i, "the");
if ((found != NULL) && (found == a+i)){
printf("%s\n", a+i);
}
}
return 0;
}

box

Re:strstrについて

#2

投稿記事 by box » 18年前

「"the"の数を格納するための変数」(※)を用意します。

for文によるループの中で「"the"が見つかったら」という
判定を行なっています。そこに、(※)の値をインクリメントする
処理を加えます。

そのループを抜けたら、(※)の値を出力します。

なお、"then"などを数えないのでしたら、strstr()の第2引数の最後に
空白1文字を加えねばならないと思います。

なぎ

Re:strstrについて

#3

投稿記事 by なぎ » 18年前

strstr() は、もともと、「文字列が見つかったらその位置を、char * で返す」という仕様です。
これが、「先頭からのバイト数」になってないのは、取っつきは悪いのですが、なかなか便利なのですね。

たとえば、

int i = 0;
char *p = a;

while((p = strstr(p, "the ")) != NULL)
{
i++;
p++;
}

こんなループでカウントすることができます。

1)p の調査したい点(最初は、文字列の先頭)から、見て、最初の "the " の位置を求める
2)"the " が存在しなかったら、ループ終了(strstr() の返値が NULL になるから)
3)i をカウントアップ
4)次の調査開始点を、今見つかった、"the " の先頭の次(具体的には、今見つかった "the " の 'h' の位置)において、再度調査

こんな意味合いです。
あと、p++; で、調査開始点をずらしていますが、ここは、"the " の次("the " のスペースの次)で、 p += 4
でも良いかもしれません。

yossiy

Re:strstrについて

#4

投稿記事 by yossiy » 18年前

ご指導ありがとうございます。
友人の助けもかりてなんとかできました。

もうひとつ質問です。

[the]と[The]両方ともカウントするためには同じような文を2つけばよろしいのでしょうか?
もしくは、もっとスマートなやり方があるのですか?
教えてください!




#include <stdio.h>
#include <string.h>
int main(void)
{
int i=0,len;
char *found;
char a[/url]="Chiba University was founded in 1949, unifying several regional
former national colleges and schools such as Chiba Medical College and Chiba
Normal School. Its fundamental mission since then has been, as encapsulated by
the inscription on the University Bell, ad altiora semper (always toward the
higher), to equip students with the ability to make mature and informed
judgments while nurturing and guiding their creativity. Pursuing these goals of
excellence has resulted in Chiba University becoming one of the leading
academic research centers of Japan.";

found = a;
while((found = strstr(found, "the ")) != NULL)
{
i++;
found++;
}

printf("[the]の個数%d個\n",i);
return 0;
}

なぎ

Re:strstrについて

#5

投稿記事 by なぎ » 18年前

The も the もカウントするということですが、もっと一般的に、大文字小文字の区別無くカウントするということであれば、よく使われるのは、

・元の文字列を(たとえば、toupper() などで)すべて大文字にそろえておく
・その後で、THE でカウントする

という処理です。
もちろん、事前にすべて小文字にそろえる(tolower() などで)のも同じ考え方になります。

yossiy

Re:strstrについて

#6

投稿記事 by yossiy » 18年前

ありがとうございました。

がんばって勉強します。

閉鎖

“C言語何でも質問掲示板” へ戻る