C# Crawler
Sagiv Hadaya is crawling in C#...just for fun




 

play with strings the way C/C++ programmers used to.

I have been asked a lot lately, how come its so different in c# to play with strings? how come we have to do many manipulations we used to before by using recursions.
while recursions are fine, they are a main source for flaws and glitches, and i say you dont have to use them.

in this article i will show you couple of tricks that will make the transit to C# from C/C++ easyier.

Lets start with a simple C++ code to reverse a string:

int Reverse(char* str)
{

if (NULL==str)return -1; //no string
int l=strlen(str)-1; //get the string length
if (1==l)return 1;

for(int x=0;x<l;x++,l--)
{
str[x]^=str[l];  //triple XOR Trick
str[l]^=str[x];  //for not using a temp
str[x]^=str[l];
}

return 0;

}

 

That is easy right? and we want to acomplish the same without any recursion needed.

here is how its done while using recursion in C#:

public static string Reverse(string str)
{
 /*termination condition*/
  if(1==str.Length)
 {
  return str;
 }
 else
 {
  return Reverse( str.Substring(1) ) + str.Substring(0,1);
 }
}
 

while the C# recursion version looks smart and easy enough, its hard for some of us to keep track of whats going on inside, and you know what.. it might just look a lot smarter to use the old pointer-like string manipulations.

This C# version of the same reverse function uses the string.Substring method for getting a char[] array of the string representation, after playing with the char array (we could search it, cut/inside, and normaly everything we done in C++ char arrays) we will convert it to a C# string back to the user:


private string Rev(string str)
{
 char[] c = str.ToCharArray (); /*convert to chararray*/
 int l = str.Length -1; 

 if (1==l)return str; //no need to reverse.

 for (int j=0;j<l;j++,l--)
   {
                c[j]^=c[l]; /*triple xor will */
                c[l]^=c[j]; /*replace c[j] with c[i]*/
                c[j]^=c[l]; /*without a temp var*/
   }
 
 string s=new string(c); /*convert back to string*/
 return s;
}

you can see here, that after converting the string to a char array, basically we are doing much of the C++ coding styles we used to, we are promissed by the .NET that the string x that is provided will not be NULL, and so we are gurenteed to not fall for that here.

Lets have one more.

There is an old interview question in C++ that goes like: "implement that atoi STL function (the function that gets a char* that represent (or not) a number and returns an integer".

in C++ the code might look like:

int atoi(char* str)
{
int sign=1;
int x=0; //counter
if (NULL==str)return 0; //might confuse

if ('-'==str[0]){sign=-1; x=1;}

int TheNumber=0;
int tmp;

int l=strlen(str)-1;

 for(;x<=l;x++)
  {
     if ((str[x]>='0')&&(str[x]<='9'))
 {
 tmp=(str[x]-'0');//0 is the 0 index
 TheNumber=TheNumber*10+tmp; //to enlarge the number by *10 every time
 }
  }

TheNumber*=sign;

return TheNumber;
}

In this example, the string "12mn4" will produce 124 as a result, meaning - comming across a non-number char will not break the loop.

To look realy non-smart, the same code could be writen in C# like this:

public static int atoi(string str)
{
 return int.Parse(str);
}

To look smarter in a C# interview, you might wanna consider writing it like so:

public static int atoi(string str)
{
int sign=1;
int TheNumber=0;
int tmp=0;
int x=0;
int l=(str.Length)-1;
char[] c=str.ToCharArray();

if ('-'==c[0]){sign=-1; x=1;}

 for(;x<l;x++)
  {
     if ((c[x]>='0')&&(c[x]<='9'))
 {
 tmp=(c[x]-'0');  //0 is the 0 index
 TheNumber=TheNumber*10+tmp; //to enlarge the number by *10 every time
 }
  }

return TheNumber*sign;


}

 

 

Pretty much the same ha?

conclusion:
if you like C++, and find the string Jargon annoying while moving to C#. use ToCharArray...and play with it.


Click here to visit the Radio UserLand website.
Click to see the XML version of this web page.
Click here to send an email to the editor of this weblog.
© Copyright 2002 Sagiv Hadaya.
Last update: 8/15/2002; 10:25:42 AM.