2017-05-01 14 views
-2

このコードを変換してC++に修正するには、何か助けが必要です 私はそれを感謝します。これをC++に変換する必要があります

プログラムは、文字列の文字列insidoを検索し、見つかった場合に表示する必要があります。

#include "iostream" 
#include "string" 
#include "stdlib.h" 

using namespace std; 

int main() 
{ 
// Input Secuence 
String Entrada = "ABCDE12345"; 
// Secence to search 
String Secuencia = "DE12"; 
// Variable to store each letter and number of the secuence 
Char Buscar; 
//Flag to show if the secuence was found 
Boolean Existe = false; 

//Cicle to read all the Input Secuence 
for (int i = 0; i < Entrada.Count(); i++) { 
//The letter is stored 
Char Letra = Entrada[i]; 
// This is the first letter in the secuence to search 
Buscar = Secuencia[0]; 

//If the letter of the input secuence matchs with the first letter 
//of the secuence to search, this is the base position then search 
//if the word exist 
if (Letra == Buscar) { 

//Cicle to read all the word to search and start to compare heach letter 
//of the input secuence starting from the current position. 
for (inf x = 1; x z Secuencia.Count(); x++) { 
// Obtaining the following letter of the input secuence 
Letra = Entrada[i + x]; 
// Obtaining the following letter of the input secuence 
Buscar = Secuencia[x]; 
//Compare if the letter are equal 
if (letra == Buscar) 
{ 
      //If they are equal the falg change to "true" 
      Existe = false; 
      //Out of the cicle 
      break; 
} 
} 
      //If the word is already fin it, then leave the cicle of reading 
      if (Existe) 
      { 
        //End of the cicle 
        break; 
        } 
      } 
} 
//Show the input secuence 
Console.WriteLine("Entrada : " + Entrada); 
// SHow the output secuence 
Console.WriteLine("Secuencia a buscar : " + Secuencia); 
//Confirm if the word was found it 
if (Existe) 
{ 
    //If exist show "found the secuence searched" 
    Console.WriteLine("La Encontre"); 
} 
else { 
    //If do not exist show "an error message" 
    Console.WriteLine("No existe"); 
    } 
} 
} 

私はC++での新たなんだと私は本当に事前に

おかげ

答えて

1

std::stringは、そのメンバ関数.find()付きのサブストリングを検索する機能を持っている多くの項目で迷ってしまいました。

これを使用すると、関数全体を2行で記述することができます。ここで

#include <iostream> 
#include <string> 

int main() { 
    std::string entrada = "ABCDE12345"; 
    std::string secuencia = "DE12"; 

    std::cout << "Entrada : " << entrada << '\n' 
       << "Secuencia a buscar : " << secuencia << '\n'; 

    if (entrada.find(secuencia) != std::string::npos) { 
     std::cout << "La Encontre\n"; 
    } else { 
     std::cout << "No existe\n"; 
    } 
} 

std::string::nposは、文字列は(それ以外の場合は、サブシーケンスが始まる文字のインデックスを返します)シーケンスを含んでいない場合にはfindによって返されたセンチネル値です。

C++ 17にアクセスできる場合は、std::searchを使用して、使用するアルゴリズムを指定することもできます。リンクされた参照には、Boyer-Mooreアルゴリズムで部分文字列を検索する例があります。

+0

私は知らない。無料でC++ 17をダウンロードする方法はありますか? –

+0

@El_Master libstdC++はGCC7のリリース候補に含まれており、検索者を実装しています。また、Clangs libC++もバージョン3.9以降で実装されています。 – Corristo

+0

私はちょうどそれが働いたそれを走った、ありがとうございます。私の先生はアルゴリズムP - NPを理解する必要があるので、これを求めました。だから私は元のプログラムを変換するように頼んだのです。 –

関連する問題