Google Answers Logo
View Question
 
Q: (C programming) Matching a single char to a string ( No Answer,   1 Comment )
Question  
Subject: (C programming) Matching a single char to a string
Category: Computers > Programming
Asked by: darsenault-ga
List Price: $10.00
Posted: 19 Oct 2002 18:36 PDT
Expires: 20 Oct 2002 21:39 PDT
Question ID: 85308
I am trying to write a function to take a char and return a string as
follows: the user will enter say:  4 + 3  and I need to turn the +
into "ADD" to store it in a structure element.

struct math_operation
{
   int operand1;
   char *operation; // this is where I need the + to be ADD
   int operand2;
} message;

I've got the extraction and assignment of the two numbers working
fine.

I've got the "+" char extracted from the args:
char *tempOp;
tempOp = argv[2];

Now I need a function to convert the "+" to "ADD" (and "-" to "SUB",
"*" to "MUL" and "/" to "DIV") then put that string into its struct
member
message.operation = ??

The attempts I've made so far have just not worked. I think I've got a
pointer problem. So, what is the correct way to have a function take
the char + - * or / and return the respective string ADD SUB MUL or
DIV?

Thank you!
-David
Answer  
There is no answer at this time.

Comments  
Subject: Re: (C programming) Matching a single char to a string
From: dancorcas-ga on 19 Oct 2002 18:52 PDT
 
hi - 
ultimately your parser will need to be more complicated than what you
have suggested here because getting the second character of an array
like this:
char *tempOp; 
tempOp = argv[2]; 

may not work unless the input has been very strictly assembled - for
example:

if you use 4 + 3 it will work - but if the user types by accident: 4+3
it will not work - or consider 4+ 3 - etc...

anyway, here is one way of doing what you are asking for:

C does not have an "associate a character with a string" or a "map a
string to a string" native to the language - so you have to do this
manually - here is one way to do it:

char * LookupOperand(char X)
( 
    char *szRet = "";

    switch (X)
    {
        case '-':
            szRet = "SUB";
            break;
        case '+':
            szRet = "ADD";
            break;
        case '*':
            szRet = "MUL";
            break;
        case '/':
            szRet = "DIV";
            break;

        default:
            // error condition
            // do something here
            break;
    }
    return(szRet);
}

now you can just call this with your character - so, it would look
something like this:

    char *Operand;

    Operand = LookupOperand(argv[2][0]);

    // or you could do this
    char *tempOp;

    tempOp = argv[2];
    Operand = LookupOperand(*tempOp);

    // these two operations are the same thing.

good luck.
Dan

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy