Extracting Substrings [duplicate] - c++

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
So I have a lab problem I need help with. I need to write a function that has a name parameter that includes the person's entire name. Ex) "John Quincy Doe". From that input, my function has to return the last name with a comma, then the first name and middle initial. Ex) "Doe, John Q". I have the right idea of checking for space and then breaking them into substrings, but how do I go about doing that in C++?

That is commonly called tokenizing a string. There are multiple answers on here. I would just comment, but apparently I can't until I have 50 reputation.
C++ Tokenize String
How do I tokenize a string in C++?

Depending on whether you are allowed to use it, you could also implement your function with a regular expression that matches three parts of the name and uses the sub matches in the correct order resp. with only the first letter of the second match, e.g. the middle name.

Related

How do I regexp-match (remove) an arbitrary series of two-letter language codes separated by commas, to the right of a title? [duplicate]

This question already has answers here:
Apply Perl RegExp to Remove Parenthesis and Text at End of String
(1 answer)
Regex for Comma delimited list
(12 answers)
Closed 2 years ago.
I have a bunch of strings such as:
Super Mario Bros. 8 (En,Fr,De,Es,It)
Donald Duck in Whacky Land (En,Fr,De,Es,Sv)
Toadstool Adventures 3D (En)
Chinaland (En,De)
A title which doesn't have any such thing
...
That is, a title of a product followed by (sometimes) a list of one or more language codes in parentheses.
I really struggle to come up with a (PCRE) regexp to safely remove these from the strings in a safe manner. That is, not likely to touch the titles.
I know that ([A-Z]{1}[a-z]{1}) must be involved somewhere, to match a single language code such as "It" or "De", but how I should handle the possibility of any number of such in a row, with commas between or no comma (if it's just one), is beyond my regular expression skills.
I really wish that they had used some kind of unambiguous separator between the title part and the "metadata" part of the filenames... Then I wouldn't need to do all this manual trial-and-error removal. But they didn't.
Something like this would do it:
\([A-Z][a-z](?:,[A-Z][a-z])*\)$
https://regex101.com/r/xxNQ8h/1
Try it like this:
\(([A-Z][a-z],?)+\).*$
Online Demo

How to match all strings that has only one dot using regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to capture strings containing only one dot. String will mostly contains domain names like
test.com, fun.test.com, lesh.test.com.
I need to check only the first one and to ignore the string that has more than one dots.
How can I do this using regex?
Like this :
^[^.]+\.[^.]+$
Check explanations https://regex101.com/r/mn7Ccr/1

What's the regular expression for pipe-delimited number? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check that a String is pipe-generated numbers. There should be numbers between pipes.
Valid Strings examples: 300, 300|600.
Invalid Strings examples: 300||||600
I tried ^([\d|\d])*$. However, this still said that 300||||600 is a valid String.
Here, what you want is one number, ie \d+, followed by an undetermined number of occurrences of a pipe then a number, which would be (\|\d+)* (the pipe is escaped).
As you want it to cover the whole input this would be
^\d+(\|\d+)*$

scala regex match up to a specific string but whole string if the specific word doesn't exist [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have a question about scala regex
The thing I need to do is given a string, I need to find a sub-string up to a specific word given. For example, my regular expression looks like following
val x= "(?s)^(.*)(?=(foo|bar)".r
Then given a string, I need to find the longest sub-string until before foo or bar. This works perfectly but I would like to get the whole string if the string does not contain foo or bar at all.
Right now if I do
x.findAllIn("hello nice to meet you").toArray
it gives me an empty string but I would like to get
"hello nice to meet you" when I do that.
Does anyone have an idea how to implement that?
You can add an end-of-string assertion to the alternative:
(?s)^(.*?)(?=(foo|bar|$))
Demo

C++: Comparing two strings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
comparing two strings with comma seperated values
I am working in C++, where I have two strings:
string str1 = "1,4,8,",
str2 = "4,1,8,";
Both strings contains comma separated values. Now I just want to check whether all the elements in str1 also exist in str2, regardless of their position. Is there any direct way to check this? Do I need to write custom code for this?
As far as C++ is concerned, those strings are just sequences of characters. If you apply meaning to those characters (such as "comma separated values"), then you'll have to write some code to extract the data and deal with it.
I would do something like:
split the string on ','
convert each sequence of digits into an integer (skipping over empty elements)
insert those integers into a set (one for each input string)
compare the sets
It's up to you to determine what kind of integer to use.
Yes, you need to write custom code, although not a lot of it. Once you figure out the algorithm you can post here if you have further questions on how to implement each part.