Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to check if all the "B" to the right and the "A" to the left. No matter the position of the "#", as long as all "B" are all on one side and "A" on the other. For example:
-Correct-
BBAA#
BBA#A
BBBB#AAAA
B#BBAAA
#BBAA
-Incorrect-
AAA#BBB
AAB#ABB
B#AAB
BAA#B
#ABAB
BABA#
ABBB#AA
Someone could help me .... I already tried but I could not
thanks
code:
#include <iostream>
#include<regex>
using namespace std;
auto check = [](string &rp)->bool {
regex reg("^(B)*(A)*");
return regex_match(rp, reg);
};
int main()
{
string rp;
do{
system("cls");
cout<<"RP: ";
getline(cin, rp);
}while (!check(rp));
cout<<"\n valid"<< rp<<endl;
return 0;
}
Regex won't move the text for you it will just tell you if the pattern is correct or not. You're going to need another tool alongside regex.
Edit: this regex should work for you. It matches 0 or more #s followed by zero or more Bs 0 or more times followed by 0 or more #s followed by 0 or more As zero or more times. ^ and $ denote the beginning and end of the string.
^(#*B*)*(#*A*)*$
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I entered three inputs to the code below and got the results as follows
(1)
input: CTRL+D
result: (blank)
(2)
input: abcCTRL+D
result: (not terminated yet)
(3)
input: abc
result: abc
using namespace::std;
int main()
{
string input;
cin >> input;
cout << input << endl;
return 0;
}
I wonder why I should enter EOF twice to terminate the code in the second case
not just only one time like the first case (it terminated immediately)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
char *s = "1234560000000000078999";
unsigned int ss = strlen(s);
vector<int> num;
unsigned int i;
for (i=0;i<ss;i+=2)
{
num.push_back((s[i] - '0')*10 + (s[i+1] - '0'));
}
i'm trying to condense a string that only contains numbers and store it in a int vector
the idea is to take each couple of numbers int the string and combain them into one integer
the problem i had is with numbers that start with zero , for example 1107 only gets stored as 117 and 1100 as 110
the other problem i had is with even numbers ;
any sultions please
thank you
1107 does, indeed, get stored as 11 and 07. When you display the values, show two digits or you won't see the leading 0 on the 07. Same thing with 1100.
As to even numbers, yes, you have to look more carefully at the number of digits that you're dealing with. If ss is odd, start out by just storing the first digit. Then process the rest in pairs. So 117 would be stored as, essentially, 01 and 17.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to find all the permutations of 0123 in the string below
01210210021212333212300213231102023103130001332121230221000012333333021032112
can i have a regular expression that can give me the permutations of 0123 matching in the string ?
Also i need if there are any overlapped patters
"0123" here i want a match of [1023][1230][2301][3012]
Not regex, but C++11:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
const std::string s("01210210021212333212300213231102023103130001332121230221000012333333021032112");
const std::string ref("0123");
if(ref.length() > s.length())
{
return 0;
}
for(int i = 0; i < s.length() - ref.length(); ++i)
{
if(std::is_permutation(s.cbegin()+i, s.cbegin()+i+ref.length(), ref.cbegin()))
{
const std::string extract(s, i, ref.length());
std::cout << extract << std::endl;
}
}
return 0;
}
To be compiled for example with g++ -std=c++11 -o sample sample.cpp
If you absolutely need regex: (?=[0123]{3})(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3). which means:
(?=[0123]{3}) : positive assertion that the 4 next characters are 0, 1, 2, 3
(.) : capture first character
(?!\1) : assert that following character is not the first capture group
(.) : capture second character
(?!\1|\2) : assert that following character is neither the first nor the second capture group
etc.
A regular expression cannot do what you're asking for. It cannot generate permutations from a string.