Finding the string of number present inside brakects after a keywrod in a file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Finding the string of number present inside brakects after a keywrod in a file

  1. #1
    Join Date
    Apr 2014
    Posts
    6

    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;
    			}			
    
    
    		}
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,701

    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()));
    Last edited by 2kaud; April 25th, 2014 at 10:05 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2014
    Posts
    6

    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.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,701

    Re: Finding the string of number present inside brakects after a keywrod in a file

    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 I'll have a look and get back.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2014
    Posts
    6

    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

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,701

    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.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2014
    Posts
    6

    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?

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,701

    Re: Finding the string of number present inside brakects after a keywrod in a file

    Yes - it just makes the process slightly slower.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,701

    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;
    Last edited by 2kaud; April 25th, 2014 at 11:25 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center