Jump to content

C++: Check a string for certain characters

Cs342

Does anyone know if it's possible to check whether a string contains a certain set of characters in a specific order? for example if i want to check if a string contains l, a and k then "black" would return positive but "kal" would return negative because it has to be in the order l, a and then k.

Have you tried turning it off and on again?

Link to comment
Share on other sites

Link to post
Share on other sites

Does anyone know if it's possible to check whether a string contains a certain set of characters in a specific order? for example if i want to check if a string contains l, a and k then "black" would return positive but "kal" would return negative because it has to be in the order l, a and then k.

my idea is to do a for loop to go through the string and if it finds an 'l' you check if next char is an 'a' and if it is you continue and check if the char after that is an 'k'. Otherwise you just use a continue to find the next 'l'. Something like that.

 

Edit: yeah, regex would be a better solution :)

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to comment
Share on other sites

Link to post
Share on other sites

It's C++, string has a "find" function built in, which returns the position of the character/string found.

You can use that in combination with "substr" function to resize the string to only contain remaining characters, use find again, etc.

e.g.

std::string s = "black";std::string temp = s;int position = 0;position = temp.find("l");if(position != std::string::npos){	temp = temp.substr(position,s.size());	position = temp.find("a");	if(position != std::string::npos){		temp = temp.substr(position,s.size());		position = temp.find("k");		if(position != std::string::npos){				return true;		}	}}return false; 

And to check for 3 characters in a row you just need

std::string s = "black";std::string temp = s;int position = 0;position = temp.find("lac"); // returns location of lacposition = temp.find("lak"); // returns std::string::npos
Link to comment
Share on other sites

Link to post
Share on other sites

Regular expressions will be very useful, and should lead to a very tight and efficient solution, that will also be easier to write than using a bunch of if statements.

Link to comment
Share on other sites

Link to post
Share on other sites

Regular expressions will be very useful, and should lead to a very tight and efficient solution, that will also be easier to write than using a bunch of if statements.

could you provide an example, I've never got my head around them completely
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

×