2017-05-01 2 views
-2

이 코드를 변환하여 C++에 수정하는 데 도움이 필요합니다. 감사하겠습니다.이 코드를 C++로 변환하는 데 도움이 필요합니다.

프로그램은 문자열의 insid 문자열을 검색하여 찾은 경우 표시해야합니다.

#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()와 문자열을 검색 할 수있는 기능을 가지고 사전에 항목

감사의 많이 잃었어요.

전체 기능은 두 줄로 작성 될 수 있습니다. 여기

#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 GCC7의 릴리스 후보에 포함 된 libstdC++는 검색 프로그램을 구현하며, 3.9 버전 이후에는 Clangs libC++도 지원합니다. – Corristo

+0

방금 ​​뛰었습니다. 고마워요. 우리 선생님은 P - NP 알고리즘을 이해해야하기 때문에이 점을 물어 보았습니다. 그것이 내가 원래의 프로그램을 개조하도록 요청한 이유입니다. –

관련 문제