Thread: How can I extract part of a string, modify it, and re-insert it?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    5

    How can I extract part of a string, modify it, and re-insert it?

    I have the following string:

    "Some Text Here 2503005 JOHN SMITH - Microsoft Inter"

    I need to extract JOHN SMITH and Convert it to John Smith

    Then save it back to a string as:
    "Some Text Here 2503005 John Smith - Microsoft Inter"

    The number is dynamic, but will always be the same length and the rest of the text is static.

    Any ideas?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Well, your abstract algorithm is spot on so I'm compelled to ask: what have you tried?

    Soma

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    5
    To be honest I am a bit of a newbie with C, I've done some basic script manipulation but nothing too terribly advanced. This particular challenge has me stumped and I don't quite know where to start.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by itsgnd View Post
    To be honest I am a bit of a newbie with C, I've done some basic script manipulation but nothing too terribly advanced. This particular challenge has me stumped and I don't quite know where to start.
    Where is this string coming from - user, or a file, or in a char array, or ?

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    5
    It is coming from a Loadrunner script. It is the result of pulling the name of the window title. The result goes directly to a string called text_buffer. The root of my problem is that there is java script that converts the name in the title bar from all caps to a proper name. But Loadrunner detects the all caps before the java script converts it and it errors out my script because it can't find the correct window title.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Okay. I just have to ask, wouldn't it be easier to improve your script by conditionally doing a case insensitive comparison?

    Soma

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    5
    That would be nice. Loadrunner (which by the way I am using for Citrix scripting) has its own functions that are not modifiable to validate the titles of windows and there is no option to make it case insensitive. Here is an example of what I am working with:


    Code:
            ctrx_get_window_name(text_buffer, CONTINUE_ON_ERROR, CTRX_LAST);
            lr_save_string( text_buffer,"WinTitle" ); 
            ctrx_sync_on_window(lr_eval_string("{WinTitle}"), ACTIVATE, 10, 30, 961, 725, "snapshot50", CTRX_LAST);
    Line 1: This pulls the current window title and puts it into text_buffer
    Line 2: This puts the string into a parameter value of WinTitle
    Line 3: This forces a wait for the expected window that was retrieved in the first step

    The problem being that the 1st command has captured the name in all caps, then there is some javascript converting the name in the title in the browser so it is upper and lower case. So when it gets to step 3 the value in Text_buffer has the name in all caps, but the title has been converted to upper and lower case causing an timeout error waiting for a title that does not exist anymore.
    Last edited by itsgnd; 05-10-2013 at 01:15 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are any of these feasible?

    • Stop the js from doing what it's doing.
    • Delay the js from changing the title.
    • Delay the call to ctrx_get_window_name until after the js has mucked about with the title.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    5
    No to the first two. It is vendor provided code and cannot be changed. I can add a delay, but experience has shown that that is unpredictable when running a load test. As load on system goes up, so does the application response time in most cases. It also decreases the load being driven while executing a test.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Changing the string is easy to do, but the C program needs to locate the text_buffer. How to locate this text?

    This is an example of a program to do what you want, IF it can get the text_buffer for input.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 80
    
    int main(void) {
       int i,r,c,n;
       char text[10][MAX]={
          {"Some Text Here 0234567 ABE SMITH - Microsoft Inter"},
          {"Some ODD Text Here 1034568 BOB SMITH - Microsoft Inter"},
          {"Some wierd Text Here 2173456 CATHY SMITH - Microsoft Inter"},
          {"Some crazy Text Here 3268015 DAVE SMITH - Microsoft Inter"},
          {"Some Serious Text Here 4303107 ED SMITH - Microsoft Inter"},
          {"Some Funny Text Here 5483615 FRED SMITH - Microsoft Inter"},
          {"Some confused Text Here 6507084 GEORGE SMITH - Microsoft Inter"},
          {"Some misspelled Text Here 7603065 HELEN SMITH - Microsoft Inter"},
          {"Some different Text Here 8836005 IRENE SMITH - Microsoft Inter"},
          {"Some of the same Text Here 9803921 JOHN SMITH - Microsoft Inter"}
       };
    
       for(r=0;r<10;r++) {
          c=0;
          while(!isdigit(text[r][c])) ++c;
          c+=9;
          while(isalpha(text[r][c])) {
             text[r][c]=tolower(text[r][c]);
             ++c;
          }
          c+=2;
          while(isalpha(text[r][c])) {
             text[r][c]=tolower(text[r][c]);
             ++c;
          }
       }
       for(r=0;r<10;r++)
          printf("%s\n",text[r]);
    
       return 0;
    }
    Last edited by Adak; 05-10-2013 at 04:33 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you already have a race condition caused by inspecting the title string twice.

    Perhaps you could point out some useful online documentation for Loadrunner and Citrix, so we can see if you missed anything that could be used to solve your problem.
    A string API without the facility to either do case conversion or perform case insensitive comparisons is broken way beyond being a joke - it's pathetic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you find an answer to your problem, itsgnd?

    If so, what was it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify string in function
    By Ducky in forum C++ Programming
    Replies: 75
    Last Post: 06-19-2012, 04:59 PM
  2. Overload insert/extract operator
    By cool_guy in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2010, 12:29 AM
  3. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 08:06 AM
  4. Extract integer value from STL string
    By ChadJohnson in forum C++ Programming
    Replies: 10
    Last Post: 03-15-2005, 02:39 AM
  5. How to extract a hex value from a string?
    By dlaw in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2002, 02:11 PM