Click here to Skip to main content
15,867,325 members
Please Sign up or sign in to vote.
3.29/5 (3 votes)
See more:
Could someone help me with some commentary as in explain whats going on this program?

C#
        static string[] groups = { "AEIOUHWY", "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" }; 

       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;
        }

        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;
        }


        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;
        }

        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;
        }
    }
}


Thank you
Posted
Updated 19-Jan-14 11:52am
v2
Comments
Ron Beyer 19-Jan-14 17:11pm    
Commentary? Are you asking us to explain it? Or comment it? or what?
Member 10506451 19-Jan-14 17:27pm    
yerrr explain it if possible
Tomas Takac 19-Jan-14 17:26pm    
I have few comments: in getCode() you should use group.Length as the upper bound in the for loop instead of hardcoded value; isn't str.Replace("0", String.Empty) what removeZeros() does?; try to use str[i] instead of str.Substring(i, 1); maybe consider using StringBuilder if the strings you are working with are long

To develop your mind for programming, you need to learn how to study what code does as it is executed, and, imho, there's no substitute for hands-on experimentation.

Do you learn a spoken language that is new to you by just studying its grammar, and looking it up in a dictionary: or, do you learn by speaking and practicing the living language in interaction with native speakers, making mistakes, analyzing the mistakes, and formal study ?

I suggest you create a new Project (Windows Forms) in Visual Studio, add a Class to the Project which you make static and public like this:
C#
namespace TestNameSpace
{
    public static class TestCodeClass
    {
       // insert all the code you show in your example

       // add the access modifier 'public for all the methods
    }
}
Once you've done that, go to the Form, and create some test cases like this:
C#
// in the Form Load EventHandler, or somewhere in a method, or Button Click EventHandler:

// set a break-point here and single-step using F11 through the code
// observing the value of the variables in the 'getCode method
string testStr1 = TestCodeClass.getCode("L");
Console.WriteLine(testStr1);

// set a break-point here and single-step using F11 through the code
// observing the value of the variables in the 'encodeString and 'getCode methods
string testStr2 = TestCodeClass.encodeString("LRwhateverBFPVetc.");
Console.WriteLine(testStr2);
Apply the same technique to study the 'removeZeroes and 'RemoveAdjacent methods.

I have seen many students with only a month or two of regularly practicing the science and art of analyzing code on-the-run, 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.

The current tools, IDE's, for code development, like Visual Studio, offer an amazing toolset for debugging compared to what was commonly available to the mere-mortal programmer even ten years ago.

CodeProject has an excellent article by Abhisit Jana "Mastering Debugging in Visual Studio 2010 - A Beginner's Guide" [^] covering fundamentals and advanced use of Visual Studio's tools for debugging and code analysis.
 
Share this answer
 
Comments
Ron Beyer 19-Jan-14 19:22pm    
Well said, +5
thatraja 20-Jan-14 2:11am    
5!
Rahul VB 20-Jan-14 14:10pm    
Hello Sir,
I have just added an explanation for my homework. I request you to go through it and add some more explanation if i am wrong.

Thanks,
- Rahul
Karthik_Mahalingam 20-Jan-14 14:51pm    
5!
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:


C#
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:

C#
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:

C#
for (i = 0; i &lt; 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:

C#
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:

C#
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
 
Share this answer
 
Comments
Karthik_Mahalingam 20-Jan-14 14:52pm    
5!Good job
i dont know y it is downvoted.
i appreciate the effort you put to write this detailed solution..
Rahul VB 20-Jan-14 15:02pm    
I just remember my days, when we used to struggle and i was not aware of code project at that time. We used to spend time wondering about what is an "object" what does it mean to instantiate it? and other stuff. There was no one to explain. I just believe youngsters today just need a little moral boosting and they will reach heights.

and i request you to modify my explanation if something wrong.

Thanks and good night, i am already late today dint get time to study at all hahaha
Bye
- Rahul
Karthik_Mahalingam 20-Jan-14 15:17pm    
Good morning :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900