I'm in an entry level c++ class. My professor wants us to write a function that counts the number of words in a string. The stipulation to this is, A WORD IS DEFINED TO BE ANT CONTIGUOUS GROUP OF ASCII CHARS THAT START WITH THE LETTERS A THROUGH Z AND MUST BE UPPER CASE. For example, the sentence: "the Dog At5674 654 Mypaper", there would be 3 words considered in this sentence. The 3 words would be Dog, At5674, and Mypaper. Right now I'm really stumped, My c++ code isn't working :sad: I can't figure what I am missing or doing wrong to pring the result out.

Here is my function:

void wordcount()
{
	string text = "the Dog At5674 654 Mypaper";
	const int x=3;;
	int index[x];
	int count=0,i,j=0,k;
		

	for(i=0; i<text.length(); i++)
	{
		if(isupper(text[i]))
		{
			count++;  //counts number of times a upper case letter is found.
			index[i]=i;  //stores the position of that capitol letter.
		}
	}
	
	while(sizeof(index))
	{
		for(k=index[j]; !isspace(text[k]) || text[k] != '\n'; k++)
		{
			cout<<text[k];
		}
		j++;

	}
}

I've found other codes for word count, but you have to keep in mind that this is entry level c++ and we have not touched on vectors, class, istreams, etc.

Thank for the help.

Recommended Answers

All 8 Replies

ok

string text = "the Dog At5674 654 Mypaper";

instead write

char text[max_lim]="the Dog At5674 654 Mypaper";

don't forget to declare the value of the constant max_lim
then instead of the for loop u used, try

int count=1 //assuming u r starting with first word
for(i=1; text!='\0' ;i++) { //finish reading at the end of the string
if(text= ' '} {count++ } // Look for any space bar.
} //one problem is if there is more than one //space bar it will assume thr r more than one word..... u can solve it by //checking the next char if it z string, then the it will simply add one with i

void wordcount()
{
	string text =  "the Dog At5674 654 Mypaper";
	int count=0,i,j,k;
	

	for(i=0; i<text.length(); i++)
	{
		if(isupper(text[i]))
		{
			count++;  //counts number of times a upper case letter is found.
		}
	}

	cout<<"Total word count: "<<count<<endl<<endl
		<<"Words are: "<<endl;


	for(k=0; k<text.length(); k++)
	{
		if(isupper(text[k]))
		{
			for(j=k; !isspace(text[j]) && text[j]!='\0'; j++)
				cout<<text.substr(j,1);  //counts number of times a upper case letter is found.
			cout<<endl;
		}
		
	}
	cout<<endl;
	return;
}

The code works, but I tried making it where the user inputs the sentence:

string text;
	
	cout << "Enter a sentence: " << endl;
	getline(cin, text);

This doesn't work for me. Could someone help me out to get this to work? Should I be using a dynamic character pointer?

Member Avatar for iamthwee

getline(cin, text,'\n');

Well after talking to a friend in my class, our professor wants us to cin a sentence into an dynamic array. So, if the sentence is longer than the allocated memory, it can just grabs some more. All I want to do at this point is just cin the user inputed sentence, then just cout it back. For example I cin "Daniweb is an excellent place to get help." and the exact sentence is couted back out. Once I get that complete, I can just apply my logic to extract the words from the sentence. Here is what I have so far:

void wordcount()
{

	int count=0,i,j,k;
	char  temp[100];   
	char* b[1000];  
	int   n = 0;    

	cout<<Enter a sentence: "<<endl;
	while (cin >> temp)
	{
		int len = strlen(temp) + 1;   
		char* newSpace = new char[len]; 
		strcpy(newSpace, temp);        
		b[n] = newSpace;            
	    n++;
	}

	for(i=0; i<sizeof(b); i++)
		cout<<*(b+i);

	return;
}

Obviously I'm doing something wrong, because the code compiles, but when I'm done entering my sentence and I hit return, the cursor just goes to another line.

Member Avatar for iamthwee
char crap[255];
	
	std::cout << "Enter a sentence: " << std::endl;
	std::cin.getline(crap,255);
	std::cout<<crap;

Yes/no ?

yah.... bcoz ur cursor is waiting for the 100 char as ur full string watch: temp[100] ....... while inputting the characters, break the loop while '\n' will be inserted.... thus u can avoid ur enter button problem..... good luck!

yah.... bcoz ur cursor is waiting for the 100 char as ur full string watch: temp[100] ....... while inputting the characters, break the loop while '\n' will be inserted.... thus u can avoid ur enter button problem..... good luck!

How would I insert the '\n' break?

is thr any requirement how u will understand the input is done?..... assume inputting "end".... or such thing..... if thrz any, just "break" the loop.... (u r in infinity loop bcoz u said while(cin>temp)..... so as long as thr will be input, the loop will alive.... even if u press only "enter" datz another input..... read the assignment requirements carefully.


u can use getline()

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.