I do I eliminate the 2 strlens from this?

  • Thread starter Jamin2112
  • Start date
In summary: There is little point in doing anything to make the code more compact, as this would not increase readability.
  • #1
Jamin2112
986
12
I have a chunk of code that is like

Code:
  if (rt->op) // if rt is of the form rt = gx op hx
  {
  char * dgdx = deriveFromTree(rt->gx); // g'(x)
  char * dhdx = deriveFromTree(rt->hx); // h'(x)
  char thisop = *rt->op;
  if (thisop == '+' || thisop == '-')
    {
      //  ADDITION/SUBTRACTION RULE:
     //  dfdx = dgdx + thisop + dhdx
      long n = strlen(dgdx) + strlen(dhdx) + 2;
      dfdx = malloc(sizeof(char) * n); dfdx[n-1]='\0';
      dfdx = strcat(dfdx, dgdx);
      dfdx = strcat(dfdx, charToString(thisop));
      dfdx = strcat(dfdx, dhdx);
     }

and I want to do it without the strlen(dgdx) and strlen(dhdx) because they seem redundant considering that the implementation of strcat iterates over all the characters of the string again. How do I redo this overall crappy procedure?
 
Technology news on Phys.org
  • #2
Jamin2112 said:
I have a chunk of code that is like

Code:
  if (rt->op) // if rt is of the form rt = gx op hx
  {
  char * dgdx = deriveFromTree(rt->gx); // g'(x)
  char * dhdx = deriveFromTree(rt->hx); // h'(x)
  char thisop = *rt->op;
  if (thisop == '+' || thisop == '-')
    {
      //  ADDITION/SUBTRACTION RULE:
     //  dfdx = dgdx + thisop + dhdx
      long n = strlen(dgdx) + strlen(dhdx) + 2;
      dfdx = malloc(sizeof(char) * n); dfdx[n-1]='\0';
      dfdx = strcat(dfdx, dgdx);
      dfdx = strcat(dfdx, charToString(thisop));
      dfdx = strcat(dfdx, dhdx);
     }

and I want to do it without the strlen(dgdx) and strlen(dhdx) because they seem redundant considering that the implementation of strcat iterates over all the characters of the string again. How do I redo this overall crappy procedure?

So you're trying to just add or subtract two numbers by trying to incorporate the operation into a string, that takes up the appropriate amount of memory as defined by the numbers and the operation, so then you can solve it I am guessing? Or what's the end goal of this supposed to be? There are also vastly better, infinitely easier languages to do this in then C.

I suppose you could always rewrite your own strcat function that takes a character (for the operation) and two character pointers to count how many characters are in the string and then use malloc from that result, which would let avoid calling strlen twice and enable you to only have to call modified strcat once making your code much cleaner and more efficient. But does that small amount of efficiency really even matter? Are you writing for a microcontroller or a mega database or something?
 
  • #3
How about a very short expression to do everything below // ADDITION...? (except the malloc part)

n = sprintf(dfdx, "%s %c %s", dgdx, thisop, dhdx);
 
  • #4
An alternative is to use C++ and std::string rather than C and C-style strings. Then you can just add the strings, using the "+" operator:

Code:
std::string dgdx = deriveFromTree(rt.gx); // g'(x)
std::string dhdx = deriveFromTree(rt.hx); // h'(x)
std::string dfdx;
if (rt.op == '+' || rt.op == '-')
{
    //  ADDITION/SUBTRACTION RULE:
    dfdx = dgdx + rt.op + dhdx;
}
 
  • #5
serp777 said:
There are also vastly better, infinitely easier languages to do this in then C.

The point is to make things harder
 
  • #6
Svein said:
How about a very short expression to do everything below // ADDITION...? (except the malloc part)

n = sprintf(dfdx, "%s %c %s", dgdx, thisop, dhdx);

That greatly helps make my procedure more compact, but I still need a way to know how large to make buffer dfdx.
 
  • #7
Jamin2112 said:
That greatly helps make my procedure more compact, but I still need a way to know how large to make buffer dfdx.
There was a long thread recently about the advantages of making code more compact (https://www.physicsforums.com/threads/code-readability-for-higher-level-languages.816168/). The upshot was that there really isn't much advantage in making the source code more compact, especially if it makes the code more opaque to the reader.
Jamin2112 said:
I want to do it without the strlen(dgdx) and strlen(dhdx) because they seem redundant considering that the implementation of strcat iterates over all the characters of the string again.
I don't see the calls to strlen() being redundant, as you say. You need to know the lengths of the two strings that you are going to concatenate. The fact that strcat copies character-by-character doesn't obviate the need to know how large the buffer for dfdx needs to be.
 
  • #8
Jamin2112 said:
The point is to make things harder
There is little point in doing that in professional programming. A major aspect of professional programming is making things easier. From other threads, you are an aspiring C++ programmer. If that's still the case, you should stop thinking like a C programmer, or like a Java programmer. Well-written C++ has zero calls to malloc and free, and very few calls to new and delete.

Given that, ...
Jamin2112 said:
That greatly helps make my procedure more compact, but I still need a way to know how large to make buffer dfdx.
Unlike many other languages, C-style strings don't store the length as a separate property. That coupled with the need to allocate storage means there's no way around calling strlen twice for this problem. There is however something you can do since you know the lengths of the strings:
Code:
char * dgdx = deriveFromTree(rt->gx); // g'(x)
char * dhdx = deriveFromTree(rt->hx); // h'(x)
char thisop = *rt->op;
std::size_t dgdx_len = std::strlen(dgdx);  // Omit std:: if you are using C.
std::size_t dhdx_len = std::strlen(dhdx);
char *dfdx;
if ((thisop == '+') || (thisop == '-'))
{
   dfdx = new char[dgdx_len+1+dhdx_len+1]; // Use malloc instead of new in C.
   std::strcpy (dfdx, dgdx);
   dfdx[dgdx_len] = thisop;                // There's no need for calling strcat here.
   std::strcpy (dfdx+dgdx_len+1, dhdx);    // Nor here.
}
...
delete[] dfdx; // Replace with free if you are using C, but never omit this.

There are some issues with the above. What if thisop is '-' and dhdx is "a+b*x+c*x^2"? You probably want to put parentheses around the embedded dhdx. I'll leave that as an exercise for the OP. You may also want to simplify the resultant expression. That is not an exercise for the OP. It means you need a symbolic mathematics library or a symbolic mathematics tool.

You might want to rethink your use of C/C++ here.
 
Last edited:
  • #9
If you still want to use C:

Declare a local character array with space for the longest possible string plus some extra headroom: char temp[MAX_OP_SIZE];
Then do what I said above (and introduce the parentheses suggested by D H):
n = sprintf(temp, "(%s) %c (%s)", dgdx, thisop, dhdx);
Then store the result:
dfdx = strdup(temp);
And you want to check for errors:
if (dfdx==NULL)
// Run in circles, scream and shout...
 
  • #10
D H said:
There are some issues with the above. What if thisop is '-' and dhdx is "a+b*x+c*x^2"? You probably want to put parentheses around the embedded dhdx.

Good call. I can't believe I didn't think of that.

You may also want to simplify the resultant expression. That is not an exercise for the OP. It means you need a symbolic mathematics library or a symbolic mathematics tool.

I'm handrolling a tool for symbolic differentiation and don't have any plan to simplify. Right now it looks atrocious (http://codepad.org/mRv44sr1), but is close to working. The insertInTree function is what's giving me trouble (I asked a question about it here: http://stackoverflow.com/questions/...algorithm-to-insert-a-node-in-a-function-tree).
 

1. How do I eliminate the 2 strlens from this?

To eliminate the 2 strlens from a given data set or program, you can use the built-in functions or methods provided by the programming language you are using. For example, in Python you can use the .replace() method to remove the specific characters or strings from a given string.

2. What is the purpose of eliminating the 2 strlens?

The purpose of eliminating the 2 strlens is to remove unnecessary or unwanted characters or strings from a given data set or program. This can help improve the accuracy or efficiency of the program, as well as make the data more organized and easier to work with.

3. Can I use regular expressions to eliminate the 2 strlens?

Yes, regular expressions can be used to eliminate the 2 strlens from a given data set or program. Regular expressions are powerful tools for pattern matching and manipulation, making them useful for removing specific characters or strings from a larger string.

4. Are there any potential drawbacks of eliminating the 2 strlens?

One potential drawback of eliminating the 2 strlens is that it may inadvertently remove necessary or important characters or strings from the data. It is important to carefully consider which characters or strings to eliminate and to test the code thoroughly to avoid any unintended consequences.

5. Is there a specific method for eliminating the 2 strlens in all programming languages?

No, there is not a universal method for eliminating the 2 strlens in all programming languages. Different languages may have different built-in functions or methods for removing characters or strings, and regular expressions may also vary. It is important to consult the documentation for the specific programming language you are using for the most accurate and efficient method.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
Back
Top