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

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

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

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

3回答

14467閲覧

c++で特定の文字列の出現回数を求める

ShrillParasite

総合スコア31

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2018/12/22 05:06

標準入力で入力された文字列の中で、一文字(例えば'a')の出現回数を数えるコードは以下のように書けましたが、標準入力で入力された文字列sの中から同じく標準入力された文字列t(一文字とは限らない)の出現回数を数えるプログラムがどうしても書けません。どうすれば文字列tの出現回数を出力させることができるでしょうか。

c++

1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() { 7 string s; 8 int count = 0; 9 cin >> s; 10 11 for (int i = 0; i < s.size(); i++) { 12 if (s[i] == 'a') { 13 count++; 14 } 15 } 16 17 cout << count << endl; 18}

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

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

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

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

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

guest

回答3

0

同じアルゴリズムを std::string で:

C++

1#include <iostream> 2#include <string> 3 4using namespace std; 5 6static const string Alice = "Alice was beginning to get very tired of sitting by her sister" 7"on the bank, and of having nothing to do: once or twice she had peeped into the" 8"book her sister was reading, but it had no pictures or conversations in it, ‘and what" 9" is the use of a book,’ thought Alice ‘without pictures or conversations?’"; 10 11int find(const string& word) { 12 int count = 0; 13 for (string::size_type pos = 0; (pos = Alice.find(word, pos)) != string::npos; pos += word.size()) { 14 ++count; 15 } 16 return count; 17} 18 19int main(int agc,char *agv[]) { 20 if( agc != 2){ 21 return 1; 22 } 23 // 24 int n= find(agv[1]); 25 cout << n << endl; 26 // 27 return 0; 28}

投稿2018/12/22 08:29

編集2018/12/22 17:16
episteme

総合スコア16612

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

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

0

strstr関数を使うといろいろ捗ります

投稿2018/12/22 05:10

y_waiwai

総合スコア88163

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

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

0

検索される文字列は埋め込みました(ふしぎの国のアリス 序文)
パラメータに検索文字列・・・細かい部分は未検証です。^^;

usr ~/test/cpp % ./a.out or
3
usr ~/test/cpp % ./a.out but
1
usr ~/test/cpp % ./a.out book
2

cpp

1#include <iostream> 2#include <cstring> 3 4using namespace std; 5 6static const char Alice[]="Alice was beginning to get very tired of sitting by her sister" 7"on the bank, and of having nothing to do: once or twice she had peeped into the" 8"book her sister was reading, but it had no pictures or conversations in it, ‘and what" 9" is the use of a book,’ thought Alice ‘without pictures or conversations?’"; 10 11int find(const char *sptr) 12{ 13 int ret= 0; 14 // 15 size_t pos= strlen(sptr); 16 char * cp= strstr(Alice,sptr); 17 while( cp ){ 18 ret++; 19 cp += pos; 20 cp = strstr(cp,sptr); 21 } 22 return ret; 23} 24 25 26int main(int agc,char *agv[]) 27{ 28 if( agc != 2){ 29 return 1; 30 } 31 // 32 int n= find(agv[1]); 33 cout << n << endl; 34 // 35 return 0; 36}

投稿2018/12/22 07:14

cateye

総合スコア6851

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

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

ShrillParasite

2018/12/22 07:53

find関数(?)の中のstrstrのところで、「型"const char *"の値を利用して型"char*"のエンティティを初期化することはできません」というエラーが出て実行できませんでした。
cateye

2018/12/22 08:23 編集

環境(OS,コンパイラ等)は何でしょう? うちは,Linux mint + clang version 8.0.0ですが?char * cp= strstr(Alice,sptr);をconst char * cp= strstr(Alice,sptr);にしてみて下さい。 ※厳密にチェックすると、int find(const char *sptr)もstatic int find(const char *sptr)にしないと、プロトタイプがないとワーニングが出ます。で、処理は理解できましたか?
ShrillParasite

2018/12/22 09:30

visual studioです。 理解できてないですね。。。。
cateye

2018/12/22 10:50 編集

strstr()関数の仕様を調べてみましょうd^^・・・(どんな言語でも)関数(ライブラリ)を使う場合は必ずその仕様を確認してから使うようにしましょう。・・・人のソースを鵜呑みにしないd^^
cateye

2018/12/22 10:49 編集

C++らしいということでは、epistemeさんの処理のほうが綺麗だと思います。要点は、検索文字列が見つかったらその位置から文字数分ずらして再検索する・・・それを繰り返すだけです。・・・余談ですが、"aaaaaaaaaaaaa"の中から"aa"を探す時ってどうするんだろう^^? 重複を許すかどうか決めごとかなあ・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問