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
Yes, the assumption is that memory has been properly allocated.
Deletesizeof(char) is 1, by definition, so multiplying by it is bad form.
DeleteA string is not terminated by a NULL pointer, it is terminated by a NUL char. This mistake is a bug in the comment of the solution to the first question. The distinction may seem trivial but the use of the word 'pointer' instead of 'char' is worrisome.
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).
Hi Nikhil,
DeleteMuchas Gracias Mi Amigo! You make learning so effortless. Anyone can follow you and I would not mind following you to the moon coz I know you are like my north star.
So basically, I’m taking a data structures class run and long story short the teacher teaches in Java, but I only know C++.
My Task: "To implement object type quick sorter class that can sort Comparable objects. Demonstrate the class in a program that sorts an array of string objects"
Basically, I want to know what the code below actually does but in simpleton terms so I can use that information to translate this to C++.
As business tier components can be re-used by different applications, the tier’s validation logic will ensure that the data is validated properly even if the web tier is not properly validated.
Follow my new blog if you interested in just tag along me in any social media platforms!
Thank you,
Kevin
I think the bug of String copy code is that you lost the target, Maybe you want to save the address of target somewhere before you copy everything from source to target
ReplyDeleteIs it a bug??
The goal of the function is to copy the source data to target. Also, it is a copy of the pointer. So nothing gets lost. I encourage to run this test yourself.
DeleteYes, it is true. You pass the pointer as parameter, instead of pointer to pointer, so nothing gets lost!
DeleteI did run the test, just I coded using C#.
Thank you so much! This is great post, I went through almost every topic here! I got interview next week, I hope I can run into similar question LOL!
If the target and source are overlapping that string copy written above wont work. Similar to MemMove Vs Memcopy
ReplyDeleteThanks..very nice..for more string related interview questions please go through this blog adnjavainterview blog.
ReplyDeleteGood post on string manipulation.For more interview questions on strings refer this link http://techno-terminal.blogspot.in/2015/09/important-string-interview-questions.html
ReplyDeleteHi folks will share few tricky java string interview questions to help freshers.
ReplyDelete
ReplyDeletenonton video sabung ayam bangkok
Bosan Menang tidak dibayar ? judi sabung ayam
ReplyDeleteĐại lý vé máy bay Aivivu
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
vé máy bay từ mỹ về việt nam giá bao nhiêu
từ canada về việt nam quá cảnh ở đâu
Máy bay từ Hàn Quốc về Việt Nam
giá thuê máy bay từ anh về việt nam
các chuyến bay từ châu âu về việt nam
các chuyến bay từ đức về việt nam hôm nay
chuyến bay nhân đạo từ nga về việt nam
vé máy bay đi Los Angeles bao nhiêu tiền
cách ly khách sạn trọn gói