2017-09-28 4 views
-1

목표는 sentence.find('and')을 사용하여 "ands" 문자열의 모든 위치를 찾는 것입니다. int 변수도 사용해야합니다. 내가 지금까지했던 어떤C++ string.find ('')

는 :

#include <iostream> 
#include <string> 

using namespace std; 
int main() { 

    string sentence; 
    int first, second, third; 
    sentence = "My name is Ryan and I like sports and music and movies"; 
    first = sentence.find('and'); 
    second = sentence.find('and'); 
    third = sentence.find('and'); 

    cout << "Position of first and is: " << first << endl; 
    cout << "Position of second and is: " << second << endl; 
    cout << "Position of third and is: " << third << endl; 

    system("pause"); 
    return 0; 

} 

그것은 단지 3 18로 계산하지만 각 개별 "와"의 위치를 ​​찾아야합니다. 도와주세요, 사전에

감사

+0

설명서를 사용하십시오. http://www.cplusplus.com/reference/string/string/find/ –

답변

1

find의 두 번째 (분명히 선택 사항) 인수가 지정하는 곳에서 검색을 시작할 문자열; 이것을 사용하여 이미 발견 한 사건을 건너 뛸 수 있습니다.