Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing a string search that has 36 instances, I only show 4 here for sanity sake and space. Is there a better way to do this. Maybe a map?

C++
#include <string>
#include <iostream>

int Count( const std::string & str, 
           const std::string & obj ) {
    int n = 0;
	int p = 0;
	int q = 0;
	int r = 0;
    std::string ::size_type pos = 0;
    while( (pos = obj.find( str, pos )) 
                 != std::string::npos ) {
    	n++;
		p++;
		q++;
		r++;
    	pos += str.size();
    }
	return n;
	return p;
	return q;
	return r;
}

int main() {
    std::string s = "How do you do at ou";
    int n = Count( "o", s );
	int p = Count( "d", s );
	int q = Count( "u", s );
	int r = Count( "t", s );
    std::cout << n << std::endl;
	std::cout << p << std::endl;
	std::cout << q << std::endl;
	std::cout << r << std::endl;
	system ("pause");
}
Posted
Updated 7-Sep-11 7:50am
v2
Comments
DaveAuld 7-Sep-11 13:51pm    
Edit: fixed formatting (needed encoding)
CPallini 7-Sep-11 15:23pm    
Do you know p,q,r inside Count are useless?
Member 7766180 7-Sep-11 15:39pm    
What do you mean? It's returning a value that is correct.
CPallini 7-Sep-11 16:31pm    
Yes, it returns n and that's corret. On the other hand, p,q,r are plainly useless. If you drop p,q,r from the routine, nothing will change.

Frnd!

Try this!

C++
# include <iostream>
#include "afxwin.h"
using  namespace std;
int GetCount(char a,CString s);
int main()
{
	CString s = "aaabbcdfaeaaaeeeeeeeeeeeeeeeeeeeea";
	int nCount_a = GetCount('a',s);
	cout << nCount_a << endl;
	int nCount_e = GetCount('e',s);
	cout << nCount_e << endl;
	
}

int GetCount(char ch, CString str)
{
	int nCount = 0;
	int  n = 0;
	for(int i = 0; i < str.GetLength(); i++)
	{
		 
		if ( str.Find(ch,i) == i)
		{
			nCount ++;
		}
	}
	return nCount;
}</iostream>
 
Share this answer
 
Comments
Member 7766180 8-Sep-11 0:48am    
Thank You. I'm getting these errors....
Error 1 error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] \include\afx.h

2 IntelliSense: expected a declaration

3 IntelliSense: identifier "PMB_IPSTATS" is undefined \include\iphlpapi.h
To be honest, if all you are doing is counting the instances of each character, I would just sort the damn thing and count the repetitions...
 
Share this answer
 
Comments
Member 7766180 7-Sep-11 14:24pm    
Actually, I just put the characters there as a reference point. It'll be searching for strings in a buffer, so is there a better way?
I am already tired to mention this article "A Tour of the Standard Library" from classic book "The C++ Programming Language" by Bjarne Stroustrup available for free from his site. Sure, you may get some better ideas reading its 22 pages.
 
Share this answer
 
Comments
Member 7766180 7-Sep-11 15:41pm    
Thank you. A lot of informative information. I will read it.
Sergey Chepurin 7-Sep-11 15:52pm    
There is not only information (very "informative"), but also complete code samples you are probably looking for.
yes it will fail since yours uses windows standard libraries.
Since the code i have sent you uses the MFC(Microsoft Foundation Classes).
Please right click the project and choose properties.
Under "Use of MFC", check the setting selected.It will be "use Standard windows libraries".
choose "USE MFC in Shared dll". and save settings.
Now build it will be fine.
 
Share this answer
 
v2
Comments
Member 7766180 9-Sep-11 13:03pm    
Thank you so very much for your solution. However I am still getting this error...Any help is appreciated. Thanks.

error C2039: '_InitNetworkAddressControl' : is not a member of 'CShellWrapper'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900