Sign in to follow this  

[C++] I'm going mad

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
s h e e p
0 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by lude
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'.


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)

Share this post


Link to post
Share on other sites
I know it's silly, but realize that string.find('c'); is only ran once! Therefore you need to loop through the string to find the position of every char (if there is one)

sample:

int x = 0;
while(s.find('c') != string::npos)
{
//continue process the finding of 'c'
x = s.find('c'); //get position
//process
}


(note: you can shorthand write while((x = s.find('c')) != string::npos))

have fun!

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
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 ??


No; the answer you get is relative to the whole string, not the start position.


s h e e p
0 1 2 3 4

Starting from 0: character 0 is not 'e'. character 1 is not 'e'. character 2 is 'e', so return 2.
Starting from 1: character 1 is not 'e'. character 2 is 'e', so return 2.
Starting from 2: character 2 is 'e', so return 2.
Starting from 3: character 3 is 'e', so return 3.
Starting from 4: character 4 is not 'e'. We reached the end of the string, so return std::string::npos.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this