Hello,
Firstly,
Quote:
static string[] groups = { "AEIOUHWY", "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" };
- This is an array of 7 strings.
Secondly:
Quote:
static string getCode(string str)
{
string tempstr = "";
int i;
for (i = 0; i < 7; i++)
{
if (groups[i].Contains(str))
{
tempstr = i.ToString();
break;
}
}
return tempstr;
}
- This function has an input(argument)which is a "String" and an output(a return value) which is also a string.
- There are 2 scenarios:
1) You pass a string from the array: groups[] into this method.
- In this case the for loop runs for 7 times(till length of the array: group[] is reached). So instead of specifying 7 explicitly best is to say : groups.Length; to make it more generic.
- the condition is satisfied because:
Quote:
if (groups[i].Contains(str))
evaluates to be true.
- Now to the variable "tempstr" assign the index of the for loop at that instant when the condition is true which is i. You cant assign an int to a string because there is no relation between these types.
- So you need to convert it to a string. So i say i.ToString(). So i becomes a string.
Now you break out of the loop. Because there is no need to check further. If i dint add a break then further checking would be done which is not required.
- Now this "i" is returned in the form of string as a code(as the name of the method suggests getcode()).
Example: Let us say i pass "BFPV" to the method getcode like this:
Quote:
getCode("BFVP")
Its return type is a string so if i say:
string code = getCode("BFVP");
code will evaluate to be : 1. Because it is the second location in the array and a zero based index so it evaluates to be 1.
As Sir said,
Quote:
using break-points, and single-stepping, accompanied by formal instruction, and diligent study, become very "fluent" in a programming language, start to reach that wonderful point where they can, literally, read code and envision how it behaves internally.
You can evaluate any thing any piece of code, but remember when the code breaks at that line that instruction is not executed so you wont find any result associated with that statement.
Add a breakpoint at the next line so you will get the debugging results you want.
Homework : try for other values from the array : groups[]. :).
Scenario 2:
- You dont pass an element from the group[] array:
the loop executes, but condition evaluates to be false. So an empty string is returned.
Which means i can say tempstr = string.empty; So an empty string is returned. So the code is empty.
Now moving ahead, the next function:
static string encodeString(string str)
{
string tempstr = "";
int i;
for (i = 0; i < str.Length; i++)
{
tempstr = tempstr + getCode(str.Substring(i, 1));
}
return tempstr;
}
For this you need to understand the significance of Substring(). If i have a string say "Rahul".
I say "Rahul".Substring(i,1);
- this statement returns "R", because substring returns a single charecter string, the first parameter i means from where to start extracting and the next parameter means: how many to extract?
- If i = 0; the result will be "R".
Look at the code snippet:
for (i = 0; i < str.Length; i++)
{
tempstr = tempstr + getCode(str.Substring(i, 1));
}
The string "str" you passed into this function is evaluated charecter by charecter.
- Every charecter is passed to getCode to get the string code and is appended to tempstr.
- So eventually you will get an encoded string for the string you pass.
Moving ahead:
static string removeZeroes(string str)
{
string tempstr = "";
int i;
for (i = 0; i < str.Length; i++)
{
if (str.Substring(i, 1) != "0")
{
tempstr = tempstr + str.Substring(i, 1);
}
}
return tempstr;
}
- The string you passed is evaluated character by character for zeros.
- If it does not contain a zero then append that particular charecter corresponding to the index i. assign it to tempstr.
- If it contains a zero return string.Empty. Which means an empty string.
- Which means zero removed.
The next code snippet:
static string RemoveAdjacent(string str)
{
string tempstr = "";
int i;
string stringToCheckAgainst = "#";
for (i = 0; i < str.Length; i++)
{
if (str.Substring(i, 1) != stringToCheckAgainst)
{
tempstr = tempstr + str.Substring(i, 1);
stringToCheckAgainst = str.Substring(i, 1);
}
}
return tempstr;
}
- In this you pass a string let us say "Rahul". This returns "Rahul". Why?
- The condition
Quote:
if (str.Substring(i, 1) != stringToCheckAgainst)
evaluates true and "tempstr" builds up to "Rahul". And hence "Rahul" is returned.
Try out: "Rahu#","#" and other combinations what do you find and why? Just try out and please let me know.
Press f11 to single step.
Thanks,
- Rahul