0

'。'を追加したい 文字列内の別の文字以外の文字ですが、方法がわかりませんか?出来ますか?

#include <iostream>

#include <string.h>

using namespace std;

int main(int argc, char *argv[]) {

    string input;
    char dot='.';
    cin>>input;
    for(int i=0;i<input.length();i++)
    {

        if( input[i]>=65 && input[i]<=90)
                {
                    input[i]=input[i]+32;   
                }
        if( (input[i]=='a') || (input[i]=='e') || (input[i]=='i') ||  (input[i]=='o') || input[i]=='y'  || input[i]=='u' )
        {
            input.erase(i,i+1);
        }
        input[i]+=dot;
    }
    cout<<input<<endl;
    return 0;
}
4

4 に答える 4

0

あなたのコメントに基づいて、あなたはこのようなものが欲しいように思えます:

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char *argv[])
{
    std::string input;
    std::cin >> input;

    std::transform (input.begin(), input.end(), input.begin(), tolower);

    size_t i = 0;
    while (i < input.length())
    {
        switch (input[i])
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'y':
            case 'u':
            {
                size_t pos = input.find_first_not_of("aeioyu", i+1);
                if (pos == std::string::npos)
                  pos = input.length();
                input.erase(i, pos-i);
                break;
            }

            default:
            {
                input.insert(i, '.');
                i += 2;
                break;
            }
        }
    }

    std::cout << input << std::endl;
    return 0;
}
于 2012-11-26T23:36:10.823 に答える
0

コードを書き始める前に、そのコードが何をするべきかについて詳細な仕様を書く必要があります。あなたのコードでは、私は推測することしかできません:小文字に変換し(素朴に、ASCIIで26個のアクセントのない文字にしか遭遇しないふりをします)、すべての母音を削除します(これも非常に素朴に、何かが母音であるかどうかを判断するためです)英語でも自明ではありません—yet と day の y を考慮してください)、最後に各文字の後にドットを挿入します。それを行う最も明白な方法は、次のようなものです。

std::string results;
for ( std::string::const_iterator current = input.begin(),
                end = input.end();
        current != end;
        ++ current ) {
    static std::string const vowels( "aeiouAEIOU" );
    if ( std::find( vowels.begin(), vowels.end(), *current )
                != vowels.end() ) {
        results.push_back(
            tolower( static_cast<unsigned char>( *current ) ) );
    }
    results.push_back( '.' );
}

しかし、繰り返しになりますが、私はあなたが何をしようとしているのかを推測しています。

もう 1 つの方法はstd::transform、最初の文字列で を使用して、すべて小文字にすることです。この種のことを定期的に行っている場合、ToLower機能的なオブジェクトができます。それ以外の場合は、一度だけ使用できるようにするためだけに作成するのは、おそらく面倒std::transformです。

于 2012-11-27T00:15:46.270 に答える
0

cpluplus.com リファレンス ( http://www.cplusplus.com/reference/string/string/insert/ )から

// inserting into a string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str="to be question";
  string str2="the ";
  string str3="or not to be";
  string::iterator it;

  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               // to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )

  cout << str << endl;
  return 0;
}

また、次のリンクも確認してください。

http://www.cplusplus.com/reference/string/string/ http://www.cplusplus.com/reference/string/string/append/ http://www.cplusplus.com/reference/string/string/プッシュバック/

于 2012-11-27T00:01:35.153 に答える
0

この入力が必要だと思います:

Hello world!

この出力を得るには:

h.ll. w.rld!

文字列をその場で変更しようとするのではなく、単純に新しい文字列を生成できます。

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
    string input;
    getline(cin, input);
    string output;
    const string vowels = "aeiouy";
    for (int i = 0; i < input.size(); ++i) {
        const char c = tolower(input[i]);
        if (vowels.find(c) != string::npos) {
            output += '.';
        } else {
            output += c;
        }
    }
    cout << output << '\n';
    return 0;
}

ノート:

  • <cctype>ですtoupper()

  • <string.h>非推奨です。使用して<string>ください。

  • getline();で行全体を読み取ります。istream::operator>>()言葉を読む。

  • tolower()、、toupper()&cを使用します。キャラ変換用。c + 32あなたの意図を説明していません。

  • 比較が必要な場合は機能しc >= 'A' && c <= 'Z'ます。ASCII コードを使用する必要はありません。

  • const変わらないものに使います。

于 2012-11-27T00:24:36.030 に答える