Link to home
Start Free TrialLog in
Avatar of TourcherTouch
TourcherTouch

asked on

String Manipulation

Hi Experts,
I have a problem with string manipulation in C.  I am using Loadrunner, which is based on C

Briefly ...I am trying to
-Capture a URL location and save it into a parameter. (DONE)
- Now use this saved URL location (saved parameter) in a new request to the server.

PROBLEM:

-I am able to achieve all the above. However, there is one problem.
- the URL location is something like this: http://print/pdflocation/id:MSGH1234&sdID=887778/

-This is causing a problem because of the '&' in the above string. As you know, this is a special character and the new request fails over because of this XML phaser. Because '&' needs to be escaped by '&'

-This means I must manipulate the string captured before using it as input to send the new request.


-- There is no specific function to 'manipulate strings' in loadrunner.

--My problem is because the next call to the WebService uses this string to palce a request.
and as soon as it comes to the '&' it fails over...because it is not escaped.

--The only solution I see to this is:

--- is to capture this string and delete everything after '&' character and then save it in a new parameter before using it to send the next request.

OR

--find the '&' occurance in the string and replace it with '&' and save it in a new parameter.

Could you please give me your guidance on how to acheive this?
I am able to save the parameter and use it in the new request. However, it is the string maipulation I am having trouble with.
Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TourcherTouch
TourcherTouch

ASKER

Hi Mrjoltcola,

Thank you for the very quick response.
the url I need to save is dynamic and changes every time.
I tried the solution you gave me and it does not seem to work.

I think a better solution is to find the '&' in the string and replace it with '&'
Not sure how to do it though...

I'll keep you posted.

Thanks again...

>>I think a better solution is to find the '&' in the string and replace it with '&'

That is exactly what the code does. Change the print line and you will see.

The function is a demonstration of the approach. I am not sure how you tested it or used it. I did test it and it works. It is a complete program. Did you compile and run it?

  printf("before: %s\nafter: %s", url, esc);

Open in new window

>>the url I need to save is dynamic and changes every time.

So call the replacement function everytime.
Mrjoltcola,

Thank you, I am now trying to get aournd some syntax errors. This is code is throwing some exceptions...perhaps because I am using it for Loadrunner. And it is not recognising the
#include <stdio.h>...

will keep you posted.
>>#include <stdio.h>

Only needed for the printf(), you can remove it as well as the printf and the main() function.

The main() was only to demonstrate how to use the function, all you need is the escape() function itself.
SOLUTION
Avatar of Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All,
Thank you for your support to try to resolve this issue. Mrjoltcola, I've used your code as a concept and improvised it and got the solution.
Here is the solution and it works great!

int offset;

      char * position;
      char * location = "http://.xyzcompany.extranet:81/file-app/stream?id=TEwzOTk5MVg%3D&sd=28.05.09/postage/";
      char * search_str = "&";
      char * escape_str = "&amp;";
      char modified_location[1024];

       int iniLength = strlen(location);
       int finalLength = iniLength+4;
       int extraLength = iniLength - offset;
       int escapeLength = strlen(escape_str);

      position = (char *)strstr(location, search_str);

      // strstr has returned the address. Now calculate * the offset from the beginning of str
       offset = (int)(position - location + 1);

       strncpy(modified_location,location,offset-1); //Copying in modified_location all characters until the search string
       strncat(modified_location,escape_str,escapeLength); //Concatenating modified_location with the escape_str
       strncat(modified_location,&location[offset],iniLength-offset); //Concatenating modified_location with the rest of the location string