Public Group

# [C++] I'm going mad

This topic is 3916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is my code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
string word = "sheep";

int size_of_word = word.size();

int position = word.find ('e');

cout << "position = "<< position;

system("pause");

return 0;

}


I am trying to find the position of 'e' in the word "sheep". The result I get is 2 (!). I included algorithm and sorted it, but I get the same result. What's wrong??

##### Share on other sites
s h e e p
0 1 2 3 4

(Btw, characters is empty, so sorting it is pointless.)

##### Share on other sites
2 is correct, index starts at 0, so s is 0, h is 1, e is 2

##### Share on other sites
WHAT A MISTAKE, GOD!!

##### Share on other sites
Quote:
 Original post by Oluseyis h e e p0 1 2 3 4(Btw, characters is empty, so sorting it is pointless.)

yeah, I had some other code as well, but I deleted it and forgot to delete that one.

THANKS FOR YOUR HELP PEOPLE !!!

##### Share on other sites
I have an other problem now :-/

I want to print the position of the second 'e'. How do I do that?

##### Share on other sites
word.find_first_of("e", 2);

##### Share on other sites
Strings find method can take another argument which is the index to start searching at. So after you find the first 'e', use the index++ as the starting index in another search to get the second 'e'.

##### Share on other sites
So, does this mean if I have int position = word.find_first_of('e', 2); I will get 2? Hmm, yes.

But if I write ('e', 3) I get position = 3 .. Shouldn't it have been 1 ??

##### Share on other sites
Quote:
 Original post by ludeStrings find method can take another argument which is the index to start searching at. So after you find the first 'e', use the index++ as the starting index in another search to get the second 'e'.

I will try it tomorrow, it's too late now. Thanks!

What I want to do, as it's getting a bit confusing, is to find how many 'e' I have and where they are located. (position)

• 17
• 11
• 15
• 9
• 49
• ### Forum Statistics

• Total Topics
631393
• Total Posts
2999774
×