2016-10-19 4 views
-2

선생님으로부터 과제를 받았습니다. 나는 약간의 코드를 시도하지만 그것은 나를 혼란스럽게 만든다. C++에서 문자열 찾기

#include <iostream> 
using namespace std; 

char inputChecker [1000]; 
string source = "10110111000111001101110"; 
string detected; 

int main(){ 
    cout <<"Input:"; 
    cin >> inputChecker; 
    for (int i=0;i<source.size();i++){ 
     if (source[i]==inputChecker[0]){ 
      cout <<"Data " <<inputChecker <<"is exist" <<endl; 
     } 
     else if (source[i]==inputChecker[i]){ 
      cout <<"Data " <<inputChecker <<" isn't exist'" <<endl; 
     } 
    } 
} 

그래서, 내 기대 출력은, 내가 입력 10, 그것은 "데이터 (10)는 존재입니다"가 발생합니다 때 : 그래서 여기 내 코드입니다. 반복하지 않고. 나는 그것이 2 종류의 루핑을 필요로한다고 생각하지만 루프를해야할 곳을 모른다.

내 기대 출력 : 사전에

Input : 10 
Data 10 is exist 

Input : 25 
Data 25 isn't exist 

감사합니다 :)) 루프

+0

'find'를'std :: strin g'는? – NathanOliver

+0

변수를 전역 변수로 지정할 이유가 없습니다. – Slava

+0

- 네임 스페이스 std를 사용하는 것은 일반적으로 [잘못된 것으로 간주됩니다] (http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – UKMonkey

답변

1

필요가 없습니다

#include <iostream> 
using namespace std; 

int main() { 
    string source = "10110111000111001101110"; 
    string input; 
    cin >> input; 
    if (source.find(input) != string::npos) 
     cout << input << " exists\n"; 
    else 
     cout << input <<" doesn't exist\n"; 
} 

find_first_of, find_last_of 등과 같은 다른 유용한 std::string 방법에서보세요 .

+0

나는 .find가 C++에 존재한다는 것을 결코 알지 못했다. (어쨌든, 그것은 나를 많이 돕는다. 고마워! –