Sunday, August 10, 2014

strstr function in LoadRunner

The most common problem that a performance tester face with automation in LoadRunner is string manipulations. To understand and implement string manipulations, one need to know the important functions that are used to do so. the function strstr() is one of the most important functions that are used for string manipulations.

The function strstr() returns the address location of a sub-string in a main string. If the sub-string is not found in the main string, the function would return zero. Hence this function can be used to validate if a sub-string is found in the main string or not.

To understand the function better, let us consider a small example:

char MainStr[1000] = "Welcome to Performance Testing Training online";
char SubStr[10] = "Train";
char * position;

position = (char *)strstr(MainStr, SubStr);

In the above example, when a MainStr variable is declared, a variable is created and 1000 bytes are allocated and the variable "MainStr" contains the starting address location. Let us assume that in the current example the Memory address location allocated for the variable MainStr is from 12001 to 13000. Now the variable MainStr contains the address location 12001.

When the strstr() function is executed, the compiler looks for the characters in the substring starting from the first address location i.e. 12001 in this case. Now, the first character in the SubString is 'T' which would be first found at the address location,  12024 (as in Testing), however the second character in the Substring does not match with the character in the next address location. So, the compiler ignores the address location 12024 and proceeds further scanning for the character 'T' which would be found at the location 12032. All the subsequent characters do match with the characters in the subsequent address locations and hence the function strstr would the return the address location 12032. The function is type casted such that the return value is explicitly defined as character pointer.

This is the most used function for most of the string manipulations and the coming blogs illustrate the usage of this function.

I hope this information is useful. If you like the content in this blog, please click +1/ Comment. Please feel free to share with your friends.

No comments:

Post a Comment