C++: Comparing two strings [duplicate] - c++

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.

Related

C++ - How to parse a char* substring into int? [duplicate]

This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 2 years ago.
I am working on a simulate file. The file has many rows with numbers, letters and characters.
Example:
GR123,7894.5444,A,4687.5643,P
GR456,1234.6556,A,9657.5686,P
GR789,2344.3422,A,9786.8465,P
GR987,6522.6354,A,3245.5754,P
I need to take the values before A and P (for the first row, 7894.5444 and 4687.5643).
How can I parse by position this string into an int? Already tried with:
double exampleA = stoi(row.substr(6,9));
double exampleP = stoi(row.substr(18,9));
But it gives me this error: request for member ‘substr’ in ‘row’, which is of non-class type ‘char*’
Also tried:
char exampleA[9];
char exampleP[9];
memcpy(&exampleA, &row[6],sizeof(exampleA));
memcpy(&exampleP, &row[6],sizeof(exampleP));
In order to convert the values after having separated them from the row but the memcpy buffer always brings the value of exampleP with exampleA like:
A : 7894.5444
P : 4687.5643?7894.5444
request for member ‘substr’ in ‘row’, which is of non-class type ‘char*’
Yeah, you can't call substr on a pointer. Perhaps you meant to store these things in a std::string instance?
As to your second attempt, there are multiple big problems with what you wrote:
The buffers you're using are 9 bytes long, but the largest number in your example looks to be 10 bytes (4 + 1 + 4 + 1). You're overflowing your buffer and overwriting random memory in your process. Edit: or you would be if you wrote proper null-terminated strings.
Related to the above, because it sort of hides the problem, don't use memcpy to copy strings. A much better first attempt would be strncpy to copy a substring to a buffer, though you still have to manually null-terminate it.
The reason why the P line shows both entries is that your compiler seems to put exampleA after exampleP on the stack, separated by a guard character, and since you don't null-terminate your strings, trying to display exampleP spills over into exampleA, which out of sheer luck seems to be followed by a null character.
Since the data seems to be in a C string anyway, you might as well use std::strtod which has the advantage of not requiring you to copy the substring:
char* endp;
double exampleA = std::strtod(row+6, &endp);
double exampleP = std::strtod(row+18, &endp);
In both cases, you might want to double check that *endp == ',' and that no conversion error was signalled.
You could use std::strtod even with a std::string, of course. It's often the most efficient solution.
Note that strtod is locale-aware, so this might not work if the input doesn't correspond to your current locale (eg. Files using period fir decimals and commas for field separators while the locale uses comma and semicolon, respectively).
I'm not sure what you mean by converting a floating point number to an integer (truncate? round? multiply by 10,000?). So I didn't address that part of the question.

Can someone please clarify what the "<" does when comparing two strings c++ [duplicate]

This question already has answers here:
Using the less than comparison operator for strings
(2 answers)
Closed 7 years ago.
I had a past paper question involving the STL sort algorithm. And it was sorting a vector, i am aware that the sort algorithm sorts into ascending order using the "<" operator. However, I was wondering how two strings are compared? How is a value for a string found? (So i can work out what string is smaller than another in an exam.)
Strings are ordered lexicographically. Each character in the first string is compared to the equivalent character in the second string until there are two characters that don't match one another or until one string ends. For example the following statements are true:
"aaab" < "aaac" // because 'b'<'c'
"aaa" < "aaab" // by convention shorter string are smaller than larger ones
"aaa0" < "aaab" // because '0'=48 and 'b'=98
"aaaB" < "aaab" // because 'B'=66 and 'b'=98
The character comparison is performed using the numeric values of the corresponding characters (using ASCII for instance).

Extracting Substrings [duplicate]

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.

How should I tokenize a string and enter the tokens into a list, using whitespace as the delimiter? [duplicate]

This question already has answers here:
How do I tokenize a string in C++?
(37 answers)
Closed 8 years ago.
I have taken many looks, but I have not been able to find a working snippet which I would understand at my current learning level. What I'm aiming to do is again:
Take input:string input = "Eggs and Spam";
Tokenize it, and then put the tokens (together) into a list: Which I see as this: inputlist = ["Eggs", "and", "Spam"];
First, I may like to know how to (hopefully briefly) declare a list, and do the above by appending the list.
In terms of C++, I'm also curious how I could do so when using only the default libraries, as I am having trouble handling library files at the moment.
The easiest way is using Boost's Strings Algorithm library - http://www.boost.org/doc/libs/1_55_0/doc/html/string_algo/usage.html#idp206847064
Then it's as easy as:
vector<string> parts;
split( parts, "Eggs and Spam", is_any_of(" "), token_compress_on )

C++ String Comparisons [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
string comparison with the most similar string
I was wondering what the best way to go about comparing two strings for (For a certain percentage of) similarity is. EX: String 1 is "I really like to eat pie," and String 2 is "I really like to eat cheese," with a function returning "true" because more than 50% of the characters are similar.
I was thinking that I could see if each character in one string is somewhere in the other, but there's probably a more precise way to go about things. Any suggestions?
Levenshtein distance might be suitable. It tells how many single-character insertions, deletions or replacements must be made in order to transform one string into the other. You can also give different priorities to the three operations.
For a fuzzy compare like this you could split each string up into words (using strtok()) and compare the two word arrays case-insensitive using stricmp(). There is also the SOUNDEX algorithm to compare words to see if they sound the same.