Solved

replace substring in c

Posted on 2014-11-05
3
110 Views
Last Modified: 2014-11-10
I have the following

void replace_callID ( char* msg,  char* newVal)
{

      // The msg is like this
      REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
      Via: SIP/2.0/TCP [22222222222222222222222]:5060;branch=z9hG4bKdebc3b0671f2;rport\r\n
      Max-Forwards: 70\r\n
      Contact: <sip:3333333333333333@[2222222222222222222222222]:5060>\r\n
      To: <sip:555555555555555@rallye>\r\n
      From: <sip:222222222222222>;tag=493d93b081015e61-95993bb7.0\r\n
      Call-ID: 111119e0a61-114650f8-c7c298767685\r\n
      CSeq: 1 REGISTER\r\n
      Expires: 600000\r\n
      Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n

      

}

// I want to replace the Call-ID value with new value e.g.


msg before calling replace_callID :

      REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
      Via: SIP/2.0/TCP [22222222222222222222222]:5060;branch=z9hG4bKdebc3b0671f2;rport\r\n
      Max-Forwards: 70\r\n
      Contact: <sip:3333333333333333@[2222222222222222222222222]:5060>\r\n
      To: <sip:555555555555555@rallye>\r\n
      From: <sip:222222222222222>;tag=493d93b081015e61-95993bb7.0\r\n
      Call-ID: 111119e0a61-114650f8-c7c298767685\r\n
      CSeq: 1 REGISTER\r\n
      Expires: 600000\r\n
      Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
      
replace_callID ( msg, "0202020202020202020202020202")

Then msg becomes

      REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
      Via: SIP/2.0/TCP [22222222222222222222222]:5060;branch=z9hG4bKdebc3b0671f2;rport\r\n
      Max-Forwards: 70\r\n
      Contact: <sip:3333333333333333@[2222222222222222222222222]:5060>\r\n
      To: <sip:555555555555555@rallye>\r\n
      From: <sip:222222222222222>;tag=493d93b081015e61-95993bb7.0\r\n
      Call-ID: 0202020202020202020202020202\r\n
      CSeq: 1 REGISTER\r\n
      Expires: 600000\r\n
      Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n

Call_ID can be CALL_ID or call_id etc ..
0
Comment
Question by:bachra04
  • 3
3 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40425118
This function will do. I have added comments in the code. Try it, as I have not tested it myself.
void replace_callID ( char* msg,  char* newVal)
{
    // assuming that the msg will allow to hold the bigger buffer if the newVal is greater than the existing Call-ID value
    const char* callIdToken = "CALL-ID: ";
    char *upper = strdup(msg);
    char *callId = strstr(upper, callIdToken);
    // find the CALL-ID (using the UPPER CASE message to find a case insensitive string)
    if(callId)
    {
        
        char *callIdEol = strstr(callId, "\r\n");
        if(callIdEol)
        {
            // store the remaining part of the message in a variable
            char *theRest = strdup(msg + (callIdEol - upper));
            // terminate the msg string straight after the Call-ID
            msg[(callId - upper) + strlen(callIdToken)] = 0;
            // append new value
            strcat(msg, newVal);
            //append the remaining message
            strcat(msg, theRest);
            // free memory
            free(theRest);
        }
    }
    // free memory
    free(upper);
}

Open in new window

0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40425154
Sorry, forgot to capitalise the string. Use strupr():
void replace_callID ( char* msg,  char* newVal)
{
    // assuming that the msg will allow to hold the bigger buffer if the newVal is greater than the existing Call-ID value
    const char* callIdToken = "CALL-ID: ";
    char *upper = strdup(msg);
    strupr(upper);
    char *callId = strstr(upper, callIdToken);
    // find the CALL-ID (using the UPPER CASE message to find a case insensitive string)
    if(callId)
    {
        
        char *callIdEol = strstr(callId, "\r\n");
        if(callIdEol)
        {
            // store the remaining part of the message in a variable
            char *theRest = strdup(msg + (callIdEol - upper));
            // terminate the msg string straight after the Call-ID
            msg[(callId - upper) + strlen(callIdToken)] = 0;
            // append new value
            strcat(msg, newVal);
            //append the remaining message
            strcat(msg, theRest);
            // free memory
            free(theRest);
        }
    }
    // free memory
    free(upper);
}

Open in new window

If your compiler does not have strupr() implement it yourself like this:
void strupr(char *string)
{
   while(*string)
   {
       if ( *string >= 'a' && *string <= 'z' )
       {
          *string = *string - 32;
       }
       string++;
   }
}

Open in new window

BTW, this is the tested program
0
 
LVL 24

Expert Comment

by:chaau
ID: 40425219
BTW, in C++ this code will look like this:
void replace_callID ( char* msg, char* newVal)
{
    const string callIdToken = "CALL-ID: ";
    string s(msg);
    string u(s);
    transform(u.begin(), u.end(), u.begin(), ::toupper);
    size_t pos, pos1;
    if((pos = u.find(callIdToken)) != string::npos &&
       (pos1 = u.find("\r\n", pos)) != string::npos)
    {
        s.replace(pos + callIdToken.length(), pos1 - pos - callIdToken.length(), newVal);
        strcpy(msg, s.c_str());
    }
}

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question