2017-09-28 4 views
-1

L'objectif est d'utiliser sentence.find('and') pour trouver toutes les positions de "ands" dans la chaîne. La variable int doit également être utilisée.C++ string.find ('')

Ce que je l'ai fait jusqu'à présent:

#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; 

} 

Il ne calcule que 18 pour tous les 3, mais je dois trouver la position de chaque individu "et". S'il vous plaît aider,

Merci à l'avance

+0

Utilisez la documentation. http://www.cplusplus.com/reference/string/string/find/ –

Répondre

1

Le second argument (évidemment en option) à find spécifie où dans la chaîne pour commencer la recherche; utilisez-le pour ignorer les occurrences que vous avez déjà trouvées.