hey guys im working on a homework problem which requires two things
-count the total number of clumps
-determine the longest clump
i finally made my code to recognize the number of total clumps however i still cnt figure out how to determine the longest clump
any help be great!thanks:)

#include <iostream>
#include <string>
using namespace std;
int countClumps(int k, string s, string & longest);
int main ()
{
	string s, string2;
	int length;
	char play;
	do
	{
		cout << "Enter a minmum clump length of 2 or more: ";
		cin >> length;
		while (length <= 1)
		{
			cout << "ILLEGAL VALUE!!!!" << endl;
			cout << "Please Enter a minmum clump length of 2 or more:";
			cin >> length;
		}
		cout << "Enter one or more words each having at least ";
		cout << "2 characters. When you want to quit, enter any word with fewer than 2 characters." << endl;
		cin >> s;
		cout << countClumps(length, s, string2) << endl;
		cout << "string2 is" << string2 << endl;
		cout << "play again";
		cin >> play;
	}
	while (play != 'n');
	return 0;
}
int countClumps(int k, string s, string & longest)
{
	longest = "";
	//  cout<<"long is" <<longest<<endl;
	int i = 1, g = 0, total = 0;
	while (g < s.length())
	{
		while (i < s.length() && s[g] == s[i])
		{
			i++;
		}
		int y = i - g;
		int p = g;
		if (y >= k)
			total++;
		if (y > longest.length())
		{
			longest = s[g], s[y];
		}
		g = i;
	}
	return total;
	//cout <<s<<"has " << total << " clumps." << endl;
}

A couple of questions first..
Exactly what is a clump?
Exactly what is your program supposed to be doing?

The reason I ask is because you haven't really explained your program or the problem particularly clearly.

Regarding the code:
The logic in your code is difficult to understand because you've used a lot of single-letter identifiers (e.g. k, g, p, i, y etc etc). And there are no comments in the code either.

If you at least use meaningful identifier names, it makes your code much more readable and makes your logic and your intentions more obviously apparent to others. Sure, k,g,p,i and y all mean something to you at the moment because you just wrote the code. But it doesn't explain anything to any third-parties who might have to read your code (e.g. myself, the rest of the Daniweb community, your programming teacher/lecturer/professor etc. etc.).

Putting meaningful comments into your code would also allow anybody reading your code to be able to understand your intentions and may also help somebody spot any logical errors in your code.

As it stands, you could end up looking back at this code in a few months and be completely lost yourself!

Also, even if you get your program working; if your code/logic is hard to read/understand, you could well end up getting marked down by whoever is marking your homework. Which is another important thing to bear in mind!

For future reference when posting here, you should clearly explain:
1. What your program/code is supposed to be doing.
2. Any problems you are having.
OK, you explained your problem quite clearly. You want to know how to determine the largest clump....But because we don't know what a clump is...you see what I'm getting at??

Fulfilling those two points alone will go a long way towards getting you the help you need, even if your code is hard to understand.

But if you also ensure any code you post is as self-explanatory as possible by using meaningful identifier names and including code comments wherever possible; it will enable others here on Daniweb to help you a whole lot faster!

BTW: I don't want you to think I'm having a go at you here, I'm just offering some straight advice!

Cheers for now,
Jas.

Wow.. you totally lost me even before I managed to figure out what you wanted your program to do. Why even introduce int p = g; ?

Could you please try to describe in words what you want your program to do? (What is a clump?)

Emil Olofsson

sorry guys my mistake actually I used my Iphone to paste the code.
so im trying to do two things with this code
first im trying to find the total number of clumps(which i already accomplished)

second the longest clump in the string(for example if user inputs>>>aabbb so output should be bbb...

#include <iostream>
#include <string>
using namespace std;
int countClumps(int k, string s, string & longest);
int main ()
{
	string string1, string2;
	int length;
	char play;
	do
	{
		cout << "Enter a minmum clump length of 2 or more: ";
		cin >> length;
		while (length <= 1)
		{
			cout << "ILLEGAL VALUE!!!!" << endl;
			cout << "Please Enter a minmum clump length of 2 or more:";
			cin >> length;
		}
		cout << "Enter one or more words each having at least ";
		cout << "2 characters. When you want to quit, enter any word with fewer than 2 characters." << endl;
		cin >> string1;
		cout << countClumps(length, string1, string2) << endl;
		cout << "longest clump is " << string2 << endl;
		cout << "play again";
		cin >> play;
	}
	while (play != 'n');
	return 0;
}
int countClumps(int minimum_clump, string string1, string & longest)
{
	longest = "";	//to determine the longest clump
	
	int max = 1, min = 0, total = 0;	//min and max counts the string to determine the clumps
	while (min < string1.length())
	{
		while (max < string1.length() && string1[min] == string1[max]) 
		{
			max++;
		}
		int total_clump = max - min;
		if (total_clump >= minimum_clump)
			total++;
	
		min = max;	//g 
	}
	return total;	//return the total number of clumps to function

}

N clump is Letters which repeating.. For example if you pick the minimum clump length to be 2 than >>aaabcndxx<<has two clumps >>aaa<<and >>xx<< and longest clump is aaa .. I'm having trouble outputing the longest clump

OK, well just quickly, here's how I'd tackle this problem (see code below!).

NOTE: I've just blatted this function out quickly, off the top of my head in gEdit. So I haven't actually compiled or tested it, but it should be more or less correct. Call it a proof of concept if you like!

I don't usually comment code so heavily either, but hopefully my excessive commenting will allow you to understand what's going on without the need for further explanation.
So without further ado, the code:

//////////////////////////////////////////////////////////////////////////////
/// countClumps - count the number of clumps in a string
//////////////////////////////////////////////////////////////////////////////
/// \param <min_clump> Minimum clump size
/// \param <strIn> The string to check
/// \param <strOut> Output string - longest clump found
/// \return <unsigned int> The number of clumps found in strIn
//////////////////////////////////////////////////////////////////////////////
unsigned int countClumps(unsigned int min_clump, const std::string &strIn, std::string &strOut)
{
	// Counters. 'number of clumps' and 'size of largest clump'.
	unsigned int numClumpsFound=0, largestClump=0;

	// if the string is shorter than the minimum clump size, 
	// don't bother doing anything. Just return!
	if(strIn.size()<min_clump)
		return 0;

	// Loop through the input string using const_iterators
	// To explain the break condition: 
	// we don't need to check for the start of a clump past the
	// character at strIn.end()-min_clump.
	// The increment section has also been left intentionally blank
	for( std::string::const_iterator clumpStart = strIn.begin(); clumpStart <= strIn.end() - min_clump; )
	{
		// create an iterator for the end of the clump
		std::string::const_iterator clumpEnd = clumpStart+1;

		// size of current clump
		unsigned int clumpSize=0;

		// if characters at clumpStart and clumpEnd match
		// we've found a clump - move clumpEnd until the chars don't match
		while(*clumpStart==*clumpEnd)
		{
			// increment clumpSize
			if(clumpSize==0)
				clumpSize=2;
			else
				clumpSize++;
			
			// move clumpEnd 
			clumpEnd++;

			// break out if we've hit the end of the string.
			if(clumpEnd==strIn.end())
				break;
		}

		// use clumpsize to determine if any clumps were detected above
		if(clumpSize>0) // clump detected
		{
			if(clumpSize>=min_clump)
			{  // The clump is large enough to be counted as a clump
				numClumpsFound++;
			
				// if the clump was larger than the largest:
				if(clumpSize>largestClump)
				{
					// set largestClump to clumpSize
					largestClump=clumpSize;
					// clear the output string and copy the largest clump into it
					strOut.clear();
					strOut.append(clumpStart, clumpEnd);
				}
			}
			// Because a clump was detected, shift clumpStart 
			// to clumpEnd's position
			clumpStart = clumpEnd;
		}	
		else // no clump detected, increment clumpStart
		{
			clumpStart++;
		}

		// increment clumpEnd, unless we've hit the end of the string
		if(clumpEnd!=strIn.end() && clumpEnd+1!=strIn.end())
			clumpEnd++;
		else // end of string already reached
			break;
	}
	
	// return the number of clumps we found.
	return numClumpsFound;

}

As I've said, this was completely off the top of my head, so there may be a few places where the logic could be improved, or where things could be done a bit more elegantly. There may even be a few syntax errors or other caveats in there. But even if that is the case, the general logic is there.

Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.