This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Optimized version of strstr (search has constant length)
(5 answers)
Closed 7 years ago.
What is the most efficient way to count a number of occurrences of a substring in another string in C++? For example, I have a very huge string like
"GQWHIWQGHWGGEEEGQIHIGWHIQWGHIEEEGPHIQPIWGHQPWGPHEEEGQIHWPWGQHPQWGEEE"
and I want to count how often "EEE" occurs.
I could go step by step in a for loop and check every letter if it's an E and if so, count them and if there are 3 es, increment a counter, but I guess there is a more efficient way of doing this.
Maybe a string function? I just wasn't able to find or google a suitable one.
I am searching for a clean C++11 solution.
Well, if you want a fast and efficent solution, take a look at Knuth–Morris–Pratt algorithm - it takes only O(N+M) to search.
If you want something in STL style, then take a look at std::string::find
Related
This question already has answers here:
Regular expression to limit number of characters to 10
(6 answers)
Closed 3 years ago.
I have a case that I only care about the maximum number.
For instance, consider that I have to simply check that the string is: "max of 10 numeric digits", which means that it should meet the following:
It contains only numbers (resolved).
It has to be 10 digits at maximum.
I read about limiting the length, I came up with the following result:
^\d{10}$: all numerics, 10 numbers specifically.
^\d{10,20}$: all numerics, 10 - 20 length.
^\d{10,}$: all numerics, 10 at minimum.
However, ^\d{,10}$ is invalid! Is there a specific way to do it, or should I do it as ^\d{1,10}$ ?
^\d{1,10}$ or anubhava's advice in the comment might simply suffice, yet there might be other excessive expressions such as:
^(?=[0-9]).{1,10}$
^(?=[0-9])\d{1,10}$
that might work.
Demo
This question already has answers here:
Building a math expression evaluator
(2 answers)
Closed 7 years ago.
I'm implementing a calculator in C ++ that respects the priorities of parentheses. I am trying to use std :: string :: find_last_of to find the last occurrence of ( and std :: string :: find to find the first occurrence of ). Once that is done, I reckon the content of the extracted substring and continuing with the rest of the string.
For example:
1 + 1 * (9 * (11-1 + 2))
I find the last occurrence of (, the first of ) and calculating 11-1 + 2. And so it continues.
Is this the right approach to this problem or is there something better?
Thank you.
You can use Reversed Polish Notation:
You will need to convert the expression into reversed polish notation and then implement a stack based algorithm to pop and push to the final answer.
Have a look at the Shunting- Yard algorithm here: To convert the expression into RPN.
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Also Have a look at
Writing a simple equation parser
For help to implement the stack, have a look here:
C++ Calculator using Stacks and Queues
One of the standard approaches to this problem consists of 2 steps:
1) Convert your expression to Reverse Polish notation: https://en.wikipedia.org/wiki/Reverse_Polish_notation using Shunting-yard algorithm https://en.wikipedia.org/wiki/Shunting-yard_algorithm
2) Evaluate converted expression using stack.
The another way to do this:
1) Easy step: Write Backus–Naur Form of your expressions https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form
And after you have 2 options:
2a) Build Finite-State Machine: https://en.wikipedia.org/wiki/Finite-state_machine (exists a lot of tools to do this: ANTLR, ...).
2b) Recursive descent method: https://en.wikipedia.org/wiki/Recursive_descent_parser
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 )
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Write a function that returns the longest palindrome in a given string
I have a C++ assignment which wants me write a program that finds the longest palindrome in a given text. For example, the text is this: asdqerderdiedasqwertunut, my program should find tunut in the index of 19. However if input is changed into this astunutsaderdiedasqwertunutit should find astunutsa in the index of 0 instead of tunutin index of 22.
So, my problem is this. But I am a beginner at the subject, i know just string class, loops, ifs. It would be great if you could help me on this.
Thanks in advance.
The idea is very simple:
Write a function is_palindrome(string) that takes a string, and returns true if it is a palindrome and false if it is not
With that function in hand, write two nested loops cutting out different substrings from the original string. Pass each substring to is_palindrome(string), and pick the longest one among the strings returning true.
You can further optimize your program by examining longest substrings ahead of shorter ones. If you examine substrings from longest to shortest, you'll be able to return as soon as you find the first palindrome.
Dasblinkenlight's idea is pretty good, but it's faster this way:
A palindrome has either an even number of letters or odd, so you have two situations. Let's start with the even. You need to find two consecutive identical letters, and then check whether the immediately previous letter is identical to the next letter. The same in the other situation, except at first you only need one letter. I don't speak English that well, so I hope you understood. :)
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.