Finding the string of number present inside brakects after a keywrod in a file
I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
if (guard_found ==std::string::npos)
{
///NAND
std::size_t found_2 = line.find(nand_str);
if (found_2!=std::string::npos)
{
vector <string> nand_inputs;
found_2 = line.find_first_of('(', found_2 + 1);
//find first input
string first_input = line.substr( found_2 + 1, ( line.find_first_of(',', found_2) - found_2 - 1) );
nand_inputs.push_back(first_input);
//Second input
found_2 = line.find_first_of(',', found_2 + 2);
string second_input = line.substr( found_2 + 2, ( line.find_first_of(')', found_2) - found_2 - 2) ); //CHANGED "found_2 + 1" TO "found_2 + 2" AND "found_2 - 1" TO "found_2 - 2"
nand_inputs.push_back(second_input);
cout<<"first: "<<first_input;
}
}
}
Re: Finding the string of number present inside brakects after a keywrod in a file
This code snippet should give you help with the way forward,
Code:
const string nand_str = "NAND(";
const string nline = "15 = NAND(9, 6, 2, 8, 10, 23)";
size_t nand,
del;
if ((nand = nline.find(nand_str)) != string::npos)
nand += nand_str.length();
for (; nand != string::npos; nand = del)
if ((del = nline.find_first_of(",)", nand)) != string::npos)
cout << atoi((nline.substr(nand, del++ - nand)).c_str()) << endl;
PS Why are you saving the nand inputs as a vector<string>? Why not save them as a vector<int>?
ie
Code:
vector<int> nand_inputs;
....
nand_inputs.push_back(atoi((nline.substr(nand, del++ - nand)).c_str()));
Re: Finding the string of number present inside brakects after a keywrod in a file
Thanks alot. It did work for me BUT it is printing an extra space i.e. " " for the inputs which are between 1st and last input. For example, the inputs which i am getting for Second line of my text file are "3", " 6", " 5". See, there is an extra space before 6 and 5 which should not be present. I can try to remove this space by making an "if condition" and getting an another string. But is there any good and short solution for it?
PS: I am not converting the string into number because my i have to process the input names as string only in my further code.
Re: Finding the string of number present inside brakects after a keywrod in a file
Quote:
BUT it is printing an extra space i.e. " " for the inputs which are between 1st and last input
Coverting to a number doesn't have this issue :D I'll have a look and get back.
Re: Finding the string of number present inside brakects after a keywrod in a file
Thank you. But i need it into string format only :)
Re: Finding the string of number present inside brakects after a keywrod in a file
Assuming there is only one space after the , then possibly the simplest way is
Code:
const string nand_str = "NAND(";
const string nline = "15 = NAND(9, 6, 2, 8, 10, 23)";
size_t nand,
del;
if ((nand = nline.find(nand_str)) != string::npos)
nand += nand_str.length() - 1;
for (; nand != string::npos; nand = del)
if ((del = nline.find_first_of(",)", nand)) != string::npos)
cout << nline.substr(nand + 1, del++ - nand - 1) << endl;
Note that if there are more than 1 space after the , then this simple solution won't work.
Re: Finding the string of number present inside brakects after a keywrod in a file
Thanks a lot. But i am using your previous technique through which i get the input in "integer" form and then i convert it into "string". Using that way also, i am able to remove and extra whitespace. This strategy is also ok?
Re: Finding the string of number present inside brakects after a keywrod in a file
Yes - it just makes the process slightly slower.
Re: Finding the string of number present inside brakects after a keywrod in a file
This will give you the numbers as a string without the leading space(s) and without the overhead of all the copying from converting to/from an integer irrespective of how many spaces there are between them.
Code:
const string nand_str = "NAND(";
const string nline = "15 = NAND( 9,6, 2, 8, 10,23 )";
size_t nand,
del;
if ((nand = nline.find(nand_str)) != string::npos)
nand += nand_str.length();
for (nand = nline.find_first_not_of(" ", nand); nand != string::npos; nand = nline.find_first_not_of(" ", del))
if ((del = nline.find_first_of(",)", nand)) != string::npos)
cout << nline.substr(nand, del++ - nand) << endl;