Sorting through delimiting characters

This is a discussion on Sorting through delimiting characters within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to write a C++ program that will determine whether a string from a file; from the ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Sorting through delimiting characters

    Hi

    I am trying to write a C++ program that will determine whether a string from a file; from the set of characters "[]" form a group. If so, I want to print the
    text that is within the out most bracket group into another file. Some examples are:

    xxxx[xxxx]xxxx = [xxxx]
    [xxxx[xxxx]xxxx = [xxxx[xxxx]
    [xxxx[xxxx]xxxx] = [xxxx[xxxx]xxxx]
    x[x[x[xxx]xx]x]x]xx = [x[x[xxx]xx]x]

    At the moment I can do the "xxxx[xxxx]xxxx = [xxxx]" example and the reading to and from files I am ok with. I am lost on the thought process I should
    take in order to see if I have multiple brackets and whether they form a valid group.

    Any help would be great.

    Currently I have:
    Code:
    #include<iostream>
    #include<fstream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    #define OPEN	'['		
    #define CLOSE   ']'	
    
    int main()
    {
    	ifstream inData;
    	ofstream outData;				
    	char filenameIn[100];
    	char filenameOut[100];
    	char lineOfText[200];
    	char chBuffer[BUFSIZ];
            char *Start;							
            char *End;							
    	int  nChar;
    
    	cout << "Enter filename to read DATA from: ";
    	cin >> filenameIn;
    	cout << endl;
    	inData.open(filenameIn);
    	cout << "Enter filename to write DATA to: ";
    	cin >> filenameOut;
    	cout << endl;
    	outData.open(filenameOut);
    	
    	if(!inData)
    		cout << "ERROR in opening " << filenameIn << endl;
    
    	else
    		if(inData)
    		{
    			cout << "Opening file: " << filenameIn << endl << endl;
    			inData.get(lineOfText, 200);
    			cout << lineOfText << endl << endl;
    			
    			if (((Start = strchr(lineOfText, OPEN)) != '\0') && (((End = strchr(Start, CLOSE))) != '\0'))	
    			{
    				nChar = End - Start + 1;											
    				strncpy(chBuffer, Start, nChar);									
    				chBuffer[nChar] = '\0';												
    				cout << "The characters found are: " << chBuffer << endl << endl;
    				outData << chBuffer << endl;
    			}
    			else
    				if(!(Start && End))
    					cout << "None found" << endl << endl; 
    		}
    
    	inData.close();
    	outData.close();
    	cout << endl << endl << endl;
    	return 0;
    }
    Last edited by Aluvas; 11-17-2002 at 10:26 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    7
    That right, and thats is what is confusing me. I can't even think of an algorithm let alone the code for that. Finding one true set of "[]" was pretty simple but finding multiple complete sets is driving me nuts.

    So taking the last example:

    x[x[x[xxx]xx]x]x]xx = [x[x[xxx]xx]x]

    I some how have to do something like:

    Find start bracket [
    See if there is another start [
    See if there is another start [
    See if there is another start [

    If not another start bracket see if there is an ending bracket ]
    Else if no ending bracket found, FAIL, no valid word group

    If ending bracket found ], verify matching start bracket
    See if there is another ending bracket found ]
    If another ending bracket found ] ,verify matching start bracket
    See if there is another ending bracket found ]
    If another ending bracket found ] ,verify matching start bracket
    See if there is another ending bracket found ]
    If another ending bracket found ] ,verify matching start bracket

    Else if no matching start bracket found
    print text in the outmost '[]' set.

    I could do it this way but only if I know how many bracket sets I am dealing with. My problem is how would you loop this in a way that you could have 10, 20 or however many bracket sets and then only print the character within the out most valid set of brackets.

    I though I could do this but I may just give up on this and try something simpiler.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    declare an two int variables to act as counters and initialize them to zero. Use a loop to read each char from string one at a time. if char is [ then increase Lcounter by one and send char to storage. if char is ] increase Rcounter by one and send char to storage. send all other char to storage only if Lcounter > 0 and Rcounter <= Lcounter..

    Code:
    char data[80] = "x[x[x[xxx]xx]x]x]xx ";
    char newData[80];
    int i, j;
    int Rcounter = 0;
    int Lcounter = 0;
    j = 0;
    for(i = 0; i < strlen(data); i++)
    {
       if(data[i] == '[')
      { 
         ++Lcounter;
         newData[j++] = data[i];
      }
      else if(data[i] == ']')
      {
         newData[j++] = data[i];
         ++Rcounter;
      }
      else
      {
         if(Lcounter > 0 && Rcounter < Lcounter)
           newData[j++] = data[i];
      }
    }
    newData[j] = '\0';
    
    cout << newData;
    Last edited by elad; 11-18-2002 at 04:46 PM.

  4. #4
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Code:
    void findBraces(char *pc)
    {
    	int open=0, close=0, ocount=0, ccount=0, i;
    	for (i=0;pc[i] != '\0';i++)
    	{
    		if (pc[i] == '[')
    		{
    			if (ocount == -1)
    				open = i+1;
    			++ocount;
    		}
    		else if (pc[i] == ']')
    		{
    			++ccount;
    			if (ccount == ocount && ocount != 0)
    				close = i-1;
    		}
    			
    	}
    	if (close == 0)
    	{
    		close = i;
    	}
    	for (i=open;i<=close;i++)
    	{
    		if (pc[i] != '[' && pc[i] != ']')
    			cout << pc[i];
    	}
    }
    That's my take on it. Although it's late and I haven't tested that, I *think* it *should* work.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    7
    So just when I thought it couldn't be done with just loops I get proven wrong. I started looking around and found the vector library and with some of those functions I started to get things going again and then I check back here today and what do I find. That simple loops can indeed get it done. Your examples have taught me a lot about loop usage.

    Thanks for the great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21