2016-06-23 4 views
-3

を削除し、私はC++でこれを達成したいが、私はどのように行うのか分からない:考えるだから私は少し問題を抱えている2つの特定の文字

は、ランダムな数字、記号、および文字を含む文字列です。

std::string = "1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"; 

今私はそう、すべて^文字を見つけようとし、そしてそれらは、0〜9までの数字が続いている場合は、^と番号を削除しています:

"^1ghhu^7dndn^g" 

は次のようになります。

"ghhudndn^g" 

私は検索と置換/文字列から文字を消去する方法を知っているが、私はそれがハードコーディングされていない方法で、番号が続いていますかどうかを確認する方法がわかりません。どんな助けもありがとうございます。

#include <iostream> 
#include <regex> 



int main() 
{ 
    using namespace std::literals; 
    auto input = "^1ghhu^7dndn^g"s; 

    static const auto repl = std::regex { R"___(\^\d)___" }; 
    auto output = std::regex_replace(input, repl, ""); 
    std::cout << output << std::endl; 
} 
+5

あなた自身で問題を解決する必要があります。そうでなければ、あなたは良い反応を得られず、宿題とみなされ、あなたが不正行為をしようとしています。あなたはコードやエラーを持っていない場合は、間違ったウェブサイト上にあるとこれはdownvoted –

+0

その宿題ではないと私は試してコーディングを提供することができますが、私didntはなぜ私はそれが仕事をしないと私はそれを投稿する必要が知っていたどのように達成する方法を知っていないと私はdoesntはエラーを与えるので、エラーを取得する – SyxDuLappen

+0

ちょうどそれを投稿し、あなたの試みとして言及、これはあなたに良い応答を得るでしょう。私はあなたが問題を解決するのを手助けします –

答えて

1

私は、このようにそれを行うだろう

#include <string> 
#include <cctype> 
1
std::string s = "^1ghhu^7dndn^g"; 
for (int i = 0; i < s.length() - 1; ++i) 
{ 
    if (s[i] == '^' && std::isdigit(s[i + 1])) 
    { 
     s.erase(i, 2); 
     --i; 
    } 
} 

これは、これらが含まれている必要があります:

#include <iostream> 
#include <string> 
#include <utility> 
#include <iterator> 

template<class Iter, class OutIter> 
OutIter remove_escaped_numbers(Iter first, Iter last, OutIter out) { 
    for (; first != last ;) 
    { 
     auto c = *first++; 
     if (c == '^' && first != last) 
     { 
      c = *first++; 
      if (std::isdigit(c)) 
       continue; 
      else { 
       *out++ = '^'; 
       *out++ = c; 
      } 
     } 
     else { 
      *out++ = c; 
     } 
    } 
    return out; 
} 

int main() 
{ 
    using namespace std::literals; 
    auto input = "^1ghhu^7dndn^g"s; 
    auto output = std::string{}; 

    remove_escaped_numbers(input.begin(), input.end(), std::back_inserter(output)); 

    std::cout << output << std::endl; 
} 

またはこのよう:

+1

これを行う最速の方法ではありませんが、シンプルで簡単に読むことができるという利点があります。 1つの提案: ' - i'の代わりに' while(i user4581301

0

することができます単にループオーバー文字列をコピーし、望ましくない文字をスキップしながらコピーします。ここではそれを行うことは可能機能です:。

std::string filterString (std::string& s) { 
    std::string result = ""; 
    std::string::iterator it = s.begin(); 
    char c; 
    while (it != s.end()) { 
     if (*it == '^' && it != s.end() && (it + 1) != s.end()) { 
      c = *(it + 1); 
      if(c >= '0' && c <= '9') { 
       it += 2; 
       continue; 
      } 
     } 
     result.push_back(*it); 
     ++ it; 
    } 
    return result; 
} 
0

堅牢なソリューションは、C++ 11にもたらすregex libraryを使用することです

std::string input ("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"); 
std::regex rx ("[\\^][\\d]{1}"); // "[\^][\d]{1}" 
std::cout << std::regex_replace(input,rx,"woot"); 

>> 1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9wootdkkns 

これは "^" の文字([\^])を検索します1({1})桁([\d])が続き、すべての出現を"woot"に置き換えます。

1

std :: stringstreamを使用し、入力文字列を返す解決方法は、キャレット数字をクリアします。

#include <iostream> 
#include <sstream> 
#include <cctype> 

int t404() 
{ 
    std::stringstream ss; 

    std::string inStr("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"); 

    for (size_t i = 0; i<inStr.size(); ++i) 
    { 
     if(('^' == inStr[i]) && isdigit(inStr[i+1])) 
     { 
     i += 1; // skip over caret followed by single digit 
     } 
     else 
     { 
     ss << inStr[i]; 
     } 
    } 
    std::cout << inStr << std::endl;  // compare input 
    std::cout << ss.str() << std::endl; // to results 
    return 0; 
} 

出力:

1653gbdtsr362g2v3f3t52bv^hdtvsbjj; hdfuue、9^1dkkns 1653gbdtsr362g2v3f3t52bv^hdtvsbjj; hdfuueは、このコードはあなたの問題を解決することができます

+0

これを愛している: ''^' == inStr [i]'。 Cのようなプログラミングの世界では、タイプミスによるバグに対して、最も簡単で防御的な防御策の1つです。 – user4581301

0

希望9dkkns:

#include <iostream> 
#include <string> 

    int main() 
{ 
    std::string str = "^1ghhu^7dndn^g"; 
    std::string::iterator first, last; 
    for (std::string::iterator it=str.begin(); it!=str.end(); ++it) 
    { 
     if(*it == '^') 
     { 
      first = it; 
      it++; 
      while(isdigit(*it)) 
      { 
       it++; 
      } 
      last = it - 1; 
      if(first != last) 
      { 
       if((last + 1) != str.end()) 
       { 
        str.erase(first, last + 1); 
       } 
       else  
       { 
        str.erase(first, str.end()); 
        break; 
       } 
      } 
     } 
    } 
    std::cout<< str << std::endl; 
    return 0; 
} 

出力:

$ ./erase 
ghhudndn^g 
関連する問題