find all substring in a string

Hi everybody!
I have a difficulty when working with string.
For example, I have a string:
string str = "X1{V 4.3}-V1{V 3.2}";

I 'd like to read from the beginning to the end of string str and detect all string "V ".
What should I do to do that task?
Thanks for your help!
For example:
1
2
3
4
5
int count = 0;
for(size_t i = str.find ("V "); i != string::npos; i = str.find ("V ", i + 2))
{
  ++count;
}


See:
http://www.cplusplus.com/reference/string/string/find/
Hi, I am not sure if what I did is what you need, but here is what I did you can test it.

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
28
29

#include <stdio.h>
#include <string.h>

int main(void){
	char str[] = "V1{V 4.3}-V1{V 3.2}"; /* actual string */
	char buff[32] = { 0 };              /* a copy of actual string */
	char delim[] = "V";                 /* used to specify the substrings */
	char* token = NULL;                 /* used to store substring token from the actual string */
	strcpy(buff,str);
	token = strtok(buff,delim);
	/* (0) output provided data */
	if(token != NULL){
		printf("\n actual = '%s'\n",str);
		printf(" delim  = '%s'\n",delim);
	}
	/* (1) check if delim exists in the actual string */
	if(strlen(token) != strlen(str))
		printf(" substr = '%s'",token);
	else 
		printf("\n (!) error, couldn't find '%s'\n",delim);
	/* (2) proccess each substring if found any, and show it on the screen */
	while((token = strtok(NULL,delim)) != NULL){
		printf(" '%s'",token);
	}
	printf("\n");
	return 0;
}
Topic archived. No new replies allowed.