I'm programming a program in C++ (typical game) in which you need to guess a letter and it will check if it is present in a string.
For example
Secret String: I like to program.
Guess1: 'a'
Display: . .... .. .....a...
Etc.
But i don't know how to see if a character is in this secret string.
I'm using std::string (obligatory)
Any help is appreciated!
Begin by learning searching in a documentation like : http://www.cplusplus.com/reference/string/string/ . (Hint : you want to "find" something ... )
You can use find_first_of
There are several methods in std::string that would help:
find()
rfind()
find_first_of()
find_last_of()
substr()
Take a look at string::find
Related
I need an analog function to find, however I need, that it would return the last position of the searched string, in other words I need opposite function of find
Ummm... perhaps rfind, for "reverse find"?
If you had been searching the string like this forward:
foo.find("bar");
To search in reverse use rfind:
foo.rfind("bar");
For the given string:
string foo{"bar-bar-bar-bar-barbra aaaanne"};
find will return: 0 and rfind will return: 16
std::string::rfind is your saver.
I need to read a String character by character. i.e, If I have a string like CILD, then I have to read it as C and then I and so on.
I tried this with regex and also with properties, but nothing has worked out.
Please let me know a solution for the same.
thanks.
You can use the below snippet in your code to add a _ at end of every character
propertyregex property="propB" input="${input}" regexp="" replace="_" global="true"
And then split the property with dleimiter as "_".
for list="${propB}" delimiter="_" param="split"
Cheers.
Note : This worked out very well for me.
I have data that looks like this:
<value>v13772 #FBst0451145:w<up>1118</up>; P{GD3649}v13772#
v13773 #FBst0451146:w<up>1118</up>; P{GD3649}v13773#</value>
How can I process this string in XPATH to extract any and all #FBst####### numbers?
I know of the xpath matches() function... but that only returns true or false. No good if I want the matching string. I've searched around but cannot find a satisfactory answer to this problem, which is probably really common.
Thanks!
In addition to the good answer by Michael Kay, if you want to use only the replace() function, then use:
replace(.,'.*?(#FBst\d+).*','$1')
The result is:
#FBst0451145
#FBst0451146
And if you only want the numbers from the above result, use:
replace(replace(.,'.*?(#FBst\d+).*','$1'),
'[^0-9]+', ' ')
This produces:
0451145 0451146
I Assume you can also use XQuery. The get_matches() function from the FunctX module should work for you. Download the file which supports your version of XQuery. Then import the module whenever you need its functionality.
import module namespace functx = "http://www.functx.com" at "functx-1.0-doc-2007-01.xq";
functx:get-matches(string-join(//text()),'xyz')
Try
tokenize(value, '[^0-9]+')
which should return the sequence of tokens separated by sequences of non-digits.
With help from Dimitre, a working regex is:
replace(.,'.*?(#FBst\d+).*','$1 ','m')
Although it doesn't work unless a newline separates each target string, it will do for now.
Thanks everyone!
I am trying to parse an HTML string using the split method from boost. Can it be used with a string delimiter like "<td>" ? Can someone give me an example of how to do it efficiently ?
I am trying to do something like
vector <string> fields;
split( fields, str, is_any_of( "<td>" ) );
But then I understand that it is treating '<','t','d' and '>' - all characters as delims.I am trying to find a way to use a string as delim.
Looking at the documentation for split it works on a character-by-character basis, treating the string as a sequence of characters. Therefore the predicate it uses to determine if something is a delimiter can only test a single character, so if you want to split on a complete string you're going to need to use something else. A regular expression library would certainly be able to do it, but you could fairly easily hand-code one by searching for substrings.
I have a text file which I geline to a string. The file is like this: 0.2abc 0.2 .2abc .2 abc.2abc abc.2 abc0.20 .2 . 20
I wanna check the result then parse it in to separate float. The result is:0.2 0.2abc 2 20 2abc abc0.20 abc
This is expalined: check if there is 2 digit (before and after '.' (full stop)) whether with char or not. If only 1 site of the '.' is digit the '.' will be full stop.
How can I parse a STRING to separate result like that? I did use iterator to check the '.' and pos of it, but still got stuck.
The first thing you need to do is split the input in words. Easy, just don't use .getline()
but instead rely on `while (cin >> strWord ) { /* do stuff with word*/ };
The second thing is to kick out bad input words early: words of 2 characters or less, with more than one ., or with the . first or last.
You now know that the . is somewhere in the middle. find() will give you an iterator. ++ and -- give you the next and previous iterators. * gives you the character that the iterator points to. isdigit() tells you whether that character is a digit. Add ingredients together and you're done.
Seems like some fairly complicated advice above -- and not necessarily helpful.
Your question does not make it entirely clear what the end result should look like. Do you want an array of floating point numbers? Do you just want the sum? Do you want to print out the results?
If you want help with homework, the best policy is to post your own attempt and then others can help you improve it, to make it work.
One approach that might help is to try to break the string into sub-strings (tokens) and discard the junk.
Write a function that accepts a character and returns true (this is part of a floating point number) or false (it isn't).
Scan along the string using an iterator or an index.
While current char is not part of a token, skip it.
If you find a token char, while current char is part of a token, copy it to another string
etc. to get all floating point substrings.
Then you can use std::stringstream or ::atof() to convert.
Have a bit of a go and post what you can get done.
sounds like you could use some regex to extract your number.
Try this regex in order to extract the floating values within a string.
[0-9]+\.[0-9]+
Keep in mind that this won't extract integer values. ie 234abc
I don't know if there is a built-in way to use regex in c++ but i found this library with a quick google search which allows you to use regex in c++
Sounds like you should look at the "Interpreter" Design Pattern.
Or you could use the "State" Design Pattern and do it by hand.
There should be plenty of examples of both on the web.