String manipulations are a favorite topic for interview questions. They are small, easy to whiteboard and test your thinking power and logic. They are a great source for SDET interviews and allow the interviewer to dig deeper into test case generation also.
So, let's start by going over some simple string operations. I chose to use C/C++ programming language for this section as pointer understanding is critical (even if you code in the managed world of C# and JAVA). Without further ado, let's review 3 simple string functions – length, copy and concatenation.
Find the length of the string
// Simple funtion to calculate the length of a string
// Basically, keep counting the chars till the end
// of the string (aka NULL pointer) is met
int StringLength(char *input)
{
// initialize
int length = 0;
// loop through the entire string
while (*input)
{
// increment the length
length++;
// move to next char
// since *input points to the data
// input contains the actual address
// this is an example of pointer arithmetic
input++;
}
return length;
}
String copy – copy source string over target
// string copy - copy source string over target string
// can you find a possible bug in the code?
void StringCopy(char* target, char *source)
{
// loop through the source string
// and replace the characters in the target string
while (*source)
{
// assign the character
*target = *source;
// move the pointers to the next location
target++;
source++;
}
// terminate the target string
*target = '\0';
}
String concatenate - concatenate 2 strings
// string concatenate - concatenate 2 strings
void StringConcatenate(char* target, char *source)
{
// loop through the target string
// till you find the end
while (*target)
target++;
// now copy source characters to the target
while (*source)
{
// assign the character
*target = *source;
// move the pointers to the next location
target++;
source++;
}
// terminate the target string
*target = '\0';
}
All the above functions are self-explanatory. There are a few bugs and optimizations that can be done to these functions. I would like to leave that as an exercise for the readers and encourage you to post your responses.
In the next section, we will dig a little bit more deeper into strings.
The mayor bug I see (specially in the "target" string when copying or concatenating strings) is that if "target" isn't properly memory allocated it would throw an error or exception.
ReplyDeleteFor concatenation, the size of the memory allocation will be:
sizeof(char) * (target.lenght + source.lenght)
Malloc for C++ lovers, Stackalloc for C# lovers :D
String copy – copy source string over target
ReplyDelete// string copy - copy source string over target string
// can you find a possible bug in the code?
void StringCopy(char* target, char *source)
{
// loop through the source string
// and replace the characters in the target string
while (*source)
{
// assign the character
*target = *source;
// move the pointers to the next location
target++;
source++;
}
// terminate the target string
*target = '\0';
}
Well yes, I see a bug. If source is NULL you will basically slice off the target (i.e., the last line).