1

長さ 15 の文字列 i/p を取得する必要があります。最初の 2 文字はアルファベット、次の 13 桁にする必要があります。例: AB1234567891234。最初の 2 つがアルファベットのみで、残りが数字のみかどうかを確認するにはどうすればよいですか?

4

4 に答える 4

6
#include <regex>
const std::regex e("^[a-zA-Z][a-zA-Z][0-9]{13}$");
std::string str = "ab1234567890123";
if (std::regex_match (s,e))
    std::cout << "string object matched\n";
于 2013-03-30T00:06:21.127 に答える
1

やの<cctype>ように、ヘッダー ファイルで定義された関数を使用できます。isalpha()isdigit()

于 2013-03-30T00:02:05.443 に答える
1
#include <cctype>

bool is_correct(std::string const& s) {
    if (s.size() != 15) return false;
    if (!std::isalpha(string[0]) || !std::isalpha(string[1]))
        return false;
    for (std::size_t i = 2; i < 13; ++i) {
        if (!std::isdigit(string[i])) return false;
    }
    return true;
}
于 2013-03-30T00:02:38.197 に答える
0
    #include<iostream>
    #include<string>

    int main(int argc, char *argv[])
    {
    std::string n_s = "AB1234567896785";
    bool res = true;
    std::cout<<"Size of String "<<n_s.size()<<n_s.length()<<std::endl;
    int i = 0, th = 2;

     while(i < n_s.length())
      {
        if(i < th)
        {
          if(!isalpha(n_s[i]))
          {
            res = false;
            break;
          }
        }
        else
        {
          if(!isdigit(n_s[i]))
          {
            res = false;
            break;
           }
        }
        i++;
    }
    if(res)
    {
        std::cout<<"Valid String "<<std::endl;
    }
    else
    {
       std::cout<<"InValid Strinf "<<std::endl;
    }
       return 0;
   }
于 2013-03-31T13:03:06.233 に答える