parsing a line

hi I have this line in a file

AxBrCrAx....... //like this

I want to know how can I find out how many times A occured with x and also how man times r occurred? any simple solution?

thanks
Use string::find_first_of (const char* s, size_t pos = 0) const; in a loop. When you find one occurence, pass it's position +1 as the pos argument for the next call.
http://www.cplusplus.com/reference/string/string/find_first_of/
closed account (E3h7X9L8)
your char positions in your string are like (0, 1, 2, 3, 4, 5, 6, 7)

in order to find what you are searching for you need to take every adjacent pair of positions and compare them ... for example

>start by taking (0, 1) which is A and x , compare them if they are A and x then increment occurences by one

>then you will take (1, 2) which is x B , compare them they are not A x so next

>(2, 3) compare them

>(3, 4) ...

then you make a second iteration throug your string and find all r's
Create a vector of type char..store the file..and traverse through it to find the occurrence

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

int main()
{
    const std::string str = "AxBrCrAxArCrBxCxAxArAxArBrAx" ;

    std::size_t cnt_Ax = 0 ;
    std::size_t cnt_r = 0 ;

    char prev_char = 0 ;
    for( char curr_char : str )
    {
        if( prev_char == 'A' && curr_char == 'x' ) ++cnt_Ax ;
        if( curr_char == 'r' ) ++cnt_r ;
        prev_char = curr_char ;
    }

    std::cout << "cnt_Ax: " << cnt_Ax << '\n'
              << "cnt_r: " << cnt_r << '\n' ;
}
done! :)
Topic archived. No new replies allowed.