Closed Thread
Results 1 to 4 of 4

Thread: Random Data in String

  1. #1
    Shokora is offline Newbie
    Join Date
    Sep 2007
    Posts
    2
    Rep Power
    0

    Random Data in String

    I'm building a parser for mathematical functions, i'm still at the very basic but something has gone wrong and i can't find the solution. If i compile the code and i use the function 100+100 as input, it gives me this output:

    $ ./test.exe
    Enter your function please: 100+100
    function[2] is numeric: 0
    function[1] is numeric: 0
    function[0] is numeric: 1
    function[0] to function[2] is the first argument
    function[4] is numeric: 1
    function[5] is numeric: 0
    function[6] is numeric: 0
    function[4] to function[6] is the second argument
    arg1 == 10↕aDĖ" && arg2 == 100

    Please help me, because i'm really growing desperate.

    Btw, i know the things like the function array aren't ideal, but i needed to test the principle before i was really going to make it.

    Code:
    //second mathematical function parser test
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define PLUS 43
    
    int main()
    {
        char function[200];
        int i, scan1, scan2;
        char arg1[200], arg2[200];
        
        printf("Enter your function please: ");
        fgets(function,sizeof(function)-1,stdin);
        
        for(i=0;i<strlen(function);i++)
        {
            //If function[i] is a + operator
            if(function[i] == PLUS)
            {
                //Scan the string for numbers (arguments)
                for(scan1=i-1;scan1>=0;scan1--)
                {
                    /*If function[i] is not a numeric value, then break because
                    you are at the end of the argument*/
                    if(function[scan1] < 48  || function[scan1] > 58)
                    {
                        break;
                    }
                    printf("function[%d] is numeric: %d \n",scan1,(function[scan1] - 48));
                }
                printf("function[%d] to function[%d] is the first argument\n",scan1,(i-1));
                //because the loop still does scan1-- one more time
                scan1++;
                
                //scan the string for the second argument
                for(scan2=i+1;scan2<strlen(function);scan2++)
                {
                    if(function[scan2] < 48 || function[scan2] > 58)
                    {
                        break;
                    }
                    printf("function[%d] is numeric: %d \n",scan2,(function[scan2] - 48));
                }
                //because the loop still does scan2++ one more time
                scan2--;
                printf("function[%d] to function[%d] is the second argument\n",(i+1),scan2);
                
                //copy them to a buffer of their own
                strncpy(arg1,function+scan1,(i-1));
                strncpy(arg2,function+(i+1),scan2);
                
                printf("arg1 == %s && arg2 == %s",arg1,arg2);
                
            }
            
        }
        
        return 0;
    }
    Last edited by Shokora; 09-27-2007 at 12:28 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    felixme86's Avatar
    felixme86 is offline Newbie
    Join Date
    Sep 2007
    Location
    Seattle WA
    Posts
    11
    Rep Power
    0
    If this project is going to be reused you need to reorganize your code or you will become hopelessly lost. you need to tokenize your data before you can do anything else. you have the right idea but you need to make it more generic and organized.

    Code:
    //second mathematical function parser test
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    #define PLUS 43
    #define FunctionSize 200
    
    int parsenumber(char *function, int start)
    {
    	for(int i=start;i<FunctionSize;i++)
    	{
    		if(function[scan1] < 48  || function[scan1] > 58)
    		{
    			return i-1;
    		}
    	}
    }
    
    int main()
    {
        char function[FunctionSize];
        int i, scan1, scan2;
        char arg1[FunctionSize], arg2[FunctionSize];
        
        printf("Enter your function please: ");
        fgets(function,sizeof(function)-1,stdin);
    
    	// get the first digit
    	int StartofNumberOne, EndofNumberOne,
    		StartofNumberTwo, EndofNumberTwo;
    	StartofNumberOne=0;
    	EndofNumberOne=parsenumber(function,StartofNumberOne);
    	StartofNumberTwo=EndofNumberOne+2;
    	EndofNumberTwo=parsenumber(function,StartofNumberTwo);

  4. #3
    Shokora is offline Newbie
    Join Date
    Sep 2007
    Posts
    2
    Rep Power
    0
    thanks i'll try to be more consistent in the future.

    I solved my problem, I accidently didn't include string.h and strncpy(); so happens to need it... T_T

  5. #4
    kkelly's Avatar
    kkelly is offline Learning Programmer
    Join Date
    Sep 2007
    Posts
    50
    Rep Power
    0
    I think in this case the devil is in the details. Take a look at it from a more abstract perspective. In particular, to build a parser, you'll want to learn about Finite State Machines. Then on to Parsers and Lexical Analyzers. For a beginning programmer, it may prove difficult to grasp, but I think these concepts are what you are struggling with.
    Once you've looked that material over you should start with some pseudo-code.
    Also, I suggest staying away from string functions and sizeof() at all costs. It's the easiest way to avoid buffer overflow issues.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 6
    Last Post: 12-27-2010, 12:13 PM
  2. random data
    By redsox8792 in forum PHP Development
    Replies: 2
    Last Post: 12-17-2010, 05:19 PM
  3. Random generation of string, towards goal result
    By Bishop in forum General Programming
    Replies: 4
    Last Post: 03-04-2010, 02:38 AM
  4. SQL Server 2008: String or binary data would be truncated
    By Keith in forum Database & Database Programming
    Replies: 1
    Last Post: 12-03-2009, 04:27 PM
  5. Get data from a string
    By larrywcc in forum PHP Development
    Replies: 9
    Last Post: 10-14-2008, 10:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

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