Finding letters in a vector of strings

hi i need to find repeating letters in a string of vectors. what am i do wrong here i am getting the memory address (some random number) but not like the number of times 'cg' occur.


1
2
3
4
int count=0;
    for (string::iterator it = dna[0].begin(), endOfString = dna[0].end(); it != endOfString; it++)
    if ( *it ==  'cg')
      count++;
You can you find method:
 
dna[0].find("cg");


Read full description of it: http://www.cplusplus.com/reference/string/string/find/
1. The string iterator will process the string on a per-element basis (here, elements are chars). So *it is a single char ('c' or 'g' or ...). So your if-test is attempting to test a single char against a pair of them.

2. Strings in C and C++ are in "s. So "cg" makes sense, as a string. But not 'cg'

What 'cg' is, in fact, a const integer.

char c = 'cg';

1>c:\source\test.cpp(81) : warning C4305: 'initializing' : truncation from 'int' to 'char'
1>c:\source\test.cpp(81) : warning C4309: 'initializing' : truncation of constant value


char* p = 'pqrs';

1>c:\source\test.cpp(101) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int  i = 'abcd'; // allowed up to 4 chars (for 32-bit int)

	cout << 'pqrs' << endl;

	cout << hex;
	cout << i << endl;
	cout << int('a') << int('b') << int('c') << int('d') << endl;

	return 0;
}


1886483059
61626364
61626364


tfityo's suggestion -- the find method -- is going to be the easiest approach (making use of the second, optional "pos" parameter, which says where to start the search from)

Andy

Last edited on
Topic archived. No new replies allowed.