CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2014
    Posts
    3

    Need some help explaining the code

    Could someone explain whats going on in this little segment of code? what each line does if possible please



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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Need some help explaining the code

    Looks like you can encode, but never decode. It tries to convert each letter into a code, which can have 0's (which it removes) and duplicate characters next to each other 1, 11, 111 (only leave 1 in each instance) to create a string of 1-9, with no repeats
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need some help explaining the code

    To really understand, you'll need to look at the code that calls these methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured