What would the regex be [duplicate] - c

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
Here's my question I was given: Write a regular expression to recognize strings with that have any number of a's, b's and c's in the order of abc. However, any number of d's may be among the a's, b's and c's.
Positive Examples:
dddaddbcdd
dddd
Negative examples:
dabcddadbdd
because 2nd sequence starts but does not finish.
ddcdd because c without leading ab
ddaddbddcaddbcdd because 2 abc sequences
Here's what I've tried:
[^abc]+(a|b|c)*[^abc]

If I understood your question well, the idea is that a must be before b and b before c if any of them are present. Additionally there might be some d in whatever place.
If that's the case then you can use a regex like this:
^d*(?:a+d*b+d*c+d*)?$
Working demo

Related

Solving user inputted equations in C [duplicate]

This question already has answers here:
math expression evaluation - very fast - with objective-c
(6 answers)
Closed 4 years ago.
I'm trying to write a program that has the user input an equation f(x), and then solve it for any value of x. Say the equation given is 3x^4-cos(x). How would I go about replacing the x's with the desired value (let's say a) and then solve it? How do you change the caret sign to the pow() function?
Short of using an existing library, you would have to write a parser that will translate your expression into some data structure (usually a tree) that can then be used to evaluate the expression, and a scanner to split your expressions into 'words' (or tokens). Tools like bison and flex (that replaced earlier yacc and lex) are helpful in writing the parser and the scanner.

Two words inside brackets in Matlab? [duplicate]

This question already has an answer here:
What is the difference between comma separated or not MATLAB function return?
(1 answer)
Closed 6 years ago.
What's that syntax for?
[sbox idx] = sort(box);
After reading Sort array elements, I would expect to see something like this:
[sbox, idx] = sort(box);
but that's not the case as you can see (it's an open sourced project, so it's not mine code). What does it mean?
Both lines do exactly the same, like [1 2 3 4] and [1,2,3,4] define the same vector.

code won't compile generates infinite 2222s [duplicate]

This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=

How to swap two variables in one line in C? [duplicate]

This question already has answers here:
Interchanging values of two variables by xor operator using references or pointers
(2 answers)
Swapping two variable value without using third variable
(31 answers)
Closed 9 years ago.
I want to know that is there any other way of swapping 2 numbers in one line and of course without 3rd variable.
I know one way of doing this:
b=a+b-(a=b)
or
a=a+b-(b=a)
both are same(approximately). If you know then please help me out.
The frequently cited classic answer that you are probably looking for is:
a^=b^=a^=b;
But, it is technically wrong, because it changes the same variable more than once before a sequence point.
Use bit twiddling in C. Following swap two variables:
if (a != b) {
a ^= b ^= a ^= b;
}

What is the history of using i in for loops? [duplicate]

This question already has answers here:
Why are variables "i" and "j" used for counters?
(23 answers)
Closed 10 years ago.
In all the programming languages I have come across there seems to be the best practice to use variable i in for loop iterations. Usually i is followed by l in the nested loop. This seem to apply both for statically compiled and scripting languages.
What is the history of this practice? Does i mark for integer, index, or something else? Why, for example, we don't use x which would be more common, considering math background.
I've got two theories: i can stand for 1) index 2) integer (value of integer type)
It makes most sense that i stands for index because the loop is over each element of an array and each element is indexed.

Resources