Find xth character in string

Hi again.
I'm wondering if there's a function that finds the let's say forth 'c' character from a string. So if I have a string str = "abcdefcc89conecs";, and use the function like cout<<str.find_xth_of(4,'c');, it should print 10. Is there such thing ? Or do I have to use the classic find function more times ?
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/string/string/at/
Kemort, I think I wasn't clear enough. Sorry.
I would like to find the (x)th 'c' or 'e' or whatever character, from a string.
closed account (48T7M4Gy)
Unless there is a library function somewhere just count them as you go via a for loop with length().
Thanks. I'll do it that way.
Keep in mind this is case sensitive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

char GetXthChar(std::string str, char c, int occurence)
{
	int tempOccur = 0;

	for(auto it : str)
	{
		if(it == c)
		{
			if(++tempOccur == occurence)
			{
				return it;
			}
		}
	}

	return NULL;
}

int main()
{
	std::cout << GetXthChar("C++ Is Cool", 'o', 2);


	return 0;
}
closed account (48T7M4Gy)
If case doesn't matter then use upper() to convert the characters to uppercase. (or all lowercase with lower() if you prefer )
Thanks guys. Meanwhile, I also got an idea.
cout<<str.find('\t',str.find('\t',str.find('\t',str.find('\t')+1)+1)+1); It doesn't look very good but it works. I need the 4th tab character in my program. So the parameters are known. Thanks again :)
Last edited on
I have again a problem but don't want to create another topic for it. I'm tired so most likely I'm missing something obvious. Could you guys please take a look ?
1
2
string str="ab\tv";
cout<<((str.at(2) != '/t') ? ",":"")<<endl;


I don't understand why this prints the ",". The third character is a tab, so the (str.at(2) != '/t') statement is false. What am I missing again ? :(
tab == '\t'
Guzfraba wrote:
Or do I have to use the classic find function more times ?

I'd probably go with that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>

std::size_t find_nth(const std::string& s, std::size_t nth, char val)
{
    std::size_t pos = 0;
    unsigned occurrence = 0;

    while (occurrence != nth && (pos = s.find(val, pos) != std::string::npos))
        ++occurrence;

    return pos;
}

int main()
{
    std::string s = "abcdefcc89conecs";
    std::cout << find_nth(s, 4, 'c') << '\n';
}
Smac...I guess I have to go to sleep. Sorry for the stupid question. I knew it was something dumb, but not that dumb.

cire, thanks for the nice function. In my program, I only have to search the xth 'x' from a string once, and that's the fourth tab character from a string. So I'll use the little algorithm I posted above. However, if I have to do this in another program, where I don't know the parameters at compile time, I'll check the functions you guys have posted here.
Thanks again to everyone !
Topic archived. No new replies allowed.