Jump to content

problem with c++

Mr.wut

hello i need some help.

i need to add a feature to a program where i could count the number of words in sentence with more than 3 characters.

do you have some idea how to do it

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Mr.wut said:

hello i need some help.

i need to add a feature to a program where i could count the number of words in sentence with more than 3 characters.

do you have some idea how to do it

Split the sentence at every non-letter (like space, comma, dot, etc.) place these 'words' in an array, loop over the entire array and check if the word you have has more than three characters, if so; count something up.

Now display this number.

 

So look up how to:

- split strings (to an array/list/whatever)

- loop over your collection

- count characters in a string

- how to display that number.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, minibois said:

Split the sentence at every non-letter (like space, comma, dot, etc.) place these 'words' in an array, loop over the entire array and check if the word you have has more than three characters, if so; count something up.

Now display this number.

 

So look up how to:

- split strings (to an array/list/whatever)

- loop over your collection

- count characters in a string

- how to display that number.

i would do that aswell

otherwise you could use regexp 

 

in python it would look like that

import re
pattern = r"[a-zA-Z]{3,}"
text = "as asdasd asd as a a d b f "
matches = re.findall(pattern, text)
occurances = len(matches)

print("{} Occurances".format(occurances))
Spoiler

image.png.b9d206ffb212028a8b1418d113ba517f.png

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/19/2020 at 1:26 PM, Mr.wut said:

hello i need some help.

i need to add a feature to a program where i could count the number of words in sentence with more than 3 characters.

do you have some idea how to do it

 

Possible implementation:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>

int
main()
{
	const auto testString = std::string("Lorem ipsum dolor sit amet ...");
	auto testStringStream = std::stringstream(testString);
	auto words = std::vector<std::string>( std::istream_iterator<std::string>(testStringStream),
					       std::istream_iterator<std::string>() );
	words.erase( std::remove_if(words.begin(), words.end(), 
		     		    [](const std::string& str) { return str.size() <= 3; }), 
		     words.end() ); 	
	std::cout << "String contains " << words.size() << " words with more then 3 characters.\n";
	
	return 0;
}

Constructs a stringstream with the given input string, then constructs a vector of strings where each element will be a word from the input string. Then erases all words with less then 4 characters.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×