Search for all substings between "+" and "n" characters [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to change some equations from input file to a "readable" form.
Currently I need to read all numerical values (as substrings, will convert them to int later) within a string P. All the values I'm interested with are between "+" and "n" characters (in this order for one loop, and the inverted order for other loop).
I need a loop which finds all of them and save them to array (of unknown size, since I don't know how long the string P will be).
Input examples (strings P in quotes):
"+n2+-n"
First loop (reads from + to n) so substrings C are: "", "-"
The second loop (reads from n to +) so substrings E are: "2", ""
"+2n3+3n2+n"
First loop: "2", "3", ""
Second loop: "3", "2", ""
"+-n14+-11n+1"
First loop: "-", "-11"
Second loop: "14", ""
I could add "+" to the end of the P string if solution requires.
ps. If someone's have an idea how to extract a constant from the end of string seen in example 3 (+1, or any other) I would really appreciate. The hard thing is I cannot tell how long it'll be (can by +1 can be -300000 or so).

Please consider to take a look at regular expressions (in general) and the new std::regex class from c++0x (in particular).
C++0x: Regular Expressions
Regular expressions are always a elegant solution if you want to parse any more complex patterns.

I didn't really understood your question, but what I personally use whenever i have unknown-length / extremely big inputs is a linked list , that you could implement in C++ with dynamic memory allocation , which is also possible in C with malloc(), if you ever find that you need C syntax.
As with the parsing of the input, you can use a variable initialized with 1, and you just multiply it with -1 every time you get to the end of a substring, and use simple if statement that covers each case. Or you could use a char that takes either "n" or "+", and so you have to make the if statement cover only a few lines of code, after you're done with the actual parsing, and whenever your cursor gets to the above-mentioned char variable, it acts like it's done it's job for another substring.

Related

String Encode in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Does the cin function add null terminated at the end of input? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
If I declare a character array: char arr[200]
and then I subsequently use the function cin to read values into arr[200]
and I type into the command window line: abcd
Is there a null terminated: \0 automatically added to the array at the end of the input?
(I don't think so because I tested it using the cin function: cin>>abcd )
Can somebody explain it to me why?
Below is a snippet of my code I use to test
char arr[200]
int count=0;
int i=0;
cin>>arr // i type into command window:abcd
while (arr[i] != '\0')
{
count++;
i++
}
My count value will not be 4 but like 43 hence I concluded that the character array is not null terminated after the cin function
Formatted input from a std::istream into a character array will null-terminate the input, as specified in C++11 27.7.2.2.3/9:
operator>> then stores a null byte (charT()) in the next position
The code you've posted gives the expected result once the obvious syntax errors are fixed. But beware that this is very dangerous; there is no check on the length of the array, so too much input will overflow it. I strongly recommend you use the std::string class, rather than plain character arrays, for managing strings.
The code you posted in a comment via a link looks like this:
char array[20];
int length=getlength(array);
cin>>array;
reading into the array after attempting to measure the string length of the uninitialised array. This could give any result, or crash, or cause any other example of undefined behaviour.
In future, you should make sure that the code you post in your question is the same code that exhibits the behaviour you're asking about; otherwise, it's impossible to answer the question.
Yes, the input will be zero-terminated. Otherwise you wouldn't be able to, for example, print it without printing random characters after your input.

Getting char from array of strings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of strings and an array of ints. Usually when I want to get a certain char from string I use:
string[char_position];
But when I have two arrays the same way of getting char doesn't work:
string[i][char_position[i]];
How can I get certain char from array of strings?
Try it like this instead:
string[i][char_position];
You sholdn't be subscripting char_position.
In first example you are using char_position as int but in second you are using it as array. Either one of this is wrong obviously if you mean same variable in two cases. But what it looks like you want to access jth charecter of ith string. If it is that then
string[i][j]; // j is position of char in string[i]
string[i][char_position[i]]; means the instructions:
get the INTEGER from char_position array indexed at i
get the character in ith string, in the position got from the first step
This is valid and correct ONLY if char_position is an ARRAY and not a SCALAR VARIABLE.
On the other hand, if char_position IS a scalar variable THEN:
string[i][char_position] is the way to retrieve char_positionth character in the ith string, in the array of character arrays string

data annonation regular expression to check digit between specify numbers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to write a regular expression which contain format like 00:00
the 00 before : must be digit between 0-24 and 00 after : must be digit between 0-59
i have my code below but somehow some it cannot work properly.
[RegularExpression(#"[0-24]+:[0-59]", ErrorMessage = "Format was invalid")]
For Exmaple
00:59 was accepted
25:60 was not accepted
What about something simple like this?
[RegularExpression(#"(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])", ErrorMessage = "Format was invalid")]
It matches on any time from 00:00 to 23:59. Note that it does not need the leading zero as written, so will also accept times like 7:00 or 7:3 for three minutes past seven (this is consistent with how you asked your question).
If you want it to require the leading zeros, just remove the question marks...
try this "([0-9]{1,2}|100):(1?[0-9]{1,2}|200)"
try with this: \b(?:2[0-4]|1?\d):(?:5\d|[1-4]?\d)\b
with leading zero:
\b(?:2[0-4]|1\d|0?\d):(?:5\d|[1-4]\d|0?\d)\b
Why do you need to solve every problem by REGEX.You should not complicate things.Break down the code.This would make your code more readable and fluent.
The method below yields the perfect matches.
public IEnumerable<string> getValues(string s)//this method returns the valid matches from s
{
Regex r=new Regex(#"\d{1,}:\d{1,}");
foreach(Match m in r.Matches(s))
{
string[] str=m.Value.Split(':');
if((int.Parse(str[0])>=0 && int.Parse(str[0])<=24) && (int.Parse(str[1])>=0&&int.Parse(str[1])<=59))
yield return str[0]+":"+str[1];
}
}
Use the above method by using a for-each statement!
foreach(string s in getValues("5:16 100:200"))//getValues method yields the perfect match
Console.WriteLine(s);//5:16

A particularly interesting program [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
we did this problem today in math class to kill time after a quiz, and i was wondering if i could make a program along these lines
so the idea is this:
take the number 123,456,789
the first digit from the left is divisible by 1, good, continue
the first 2 digits from the left are divisible by 2, good, continue
the first 3 digits from the left are divisible by 3, good, continue
the first 4 digits from the left are NOT divisible by 4, you get a remainder, bad, restart with different numbers, or go back until one of the numbers can be replaced (the even spaces are pretty interchangeable)
the tricky part here is that you cant use the same integer twice
the only place we can be sure about is the fifth place, so we can tell that the number will look something like: _ _ _, _ 5 _, _ _ _
i want the program to print the number(s) which is(are) perfectly divisible, all the way to the ones place. im pretty sure that only one number fits this criteria, but id like to know if this is true, which is why im trying to make this program.
what i need help with is how i should go about checking if each number divided by its place has no remainder, and how to go back if it doesnt fit.
pretty new to coding..anything helps
Programming is just describing a series of steps to a computer
Edit - sorry, rereading the question it would be easier to store the value as a long integer and then increase it in a loop.
In which case you will have to understand how to get the first 3 digits by dividing by 10^6 (or whatever) and then taking only the integer part.
You migth also want the modulus operator %
You could setup a loop, that uses the modulus operator (%) to get the remainder of a number. I would setup an array with a legnth of 10, and modulus the number by 10 and put the result into the array (because you will read the numbers in reverse you need to place them into the array in reverse), until you have each digit.
I would then setup an int (named maybe value), and put the first value in the array, into this int. I would also designate another int (named maybe place) and set it equal to 1. Define a bool (named maybe flag).
I would then enter a loop so that you have the equation if(value % place == 0) flag = 0.
Then have the loop break if flag == 1, and goto a label before the loop, where you have an algorithm that generates a new number (maybe somthing as simple as adding one, or what ever you want). Also inside the loop if place == 10 and your last value in the number is 0, you need to print the number, and then jump to your label from earlier, before the loop, to pick a new number. Also don't forget that if flag == 0, and place != 10 (the number hasn't failed yet, but isn't at the end yet) , that you need to multiply the value by 10 , then add the next number in the array to be able to test the next place. And you also need to increase place by 1 each time.
You said you're new, so I'll include a few definations:
'=' sets a variable equal to somthing, '==' is used to test equality
'!=' means "not equal to". It's also used to test variables against other variables or numbers.
'%' or modulus divides two numbers but rather returns the remainder (ex. 10 % 3 = 1, because 10 / 3 = 9 remainder 1)