Manipulating Strings The Way C/C++ Programmers Use To
I have been asked a lot lately, how come it'sso different in C# to play around with strings? How come we have to doso many manipulations. We used to do this using recursions. Whilerecursions are fine, they are a main source for flaws and glitches, andI don't think you have to use them.
In this article U will show you couple of tricks that will make the transitition to C# from C/C++ easier.
Let's 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? We want to accomplish the same without any recursion needed.
Here is how it is done using recursion in c#:
public static string Reverse(string str) While the C# recursion version looks smart andeasy enough, it's hard for some of us to keep track of what is going oninside, and you know what .. it might just look a lot smarter to usethe old pointer-like string manipulations. This C# version of the same reverse functionuses the string.Substring method for getting a char[] array of thestring representation, after playing with the char array (we couldsearch it, cut/inside, and normally everything we did in C++ chararrays) we will convert it to a C# string back to the user: private string Rev(string str) if (1==l)return str; //no need to reverse. for (int j=0;j < l;j++,l–) string s=new string(c); /*convert back to string*/
{
/*termination condition*/
if(1==str.Length)
{
return str;
}
else
{
return Reverse( str.Substring(1) ) + str.Substring(0,1);
}
}
{
char[] c = str.ToCharArray (); /*convert to chararray*/
int l = str.Length -1;
{
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*/
}
return s;
}
You can see here, that after converting thestring to a char array, basically we are doing much of the C++ codingstyles we used to. We are promised by .NET that the string x that isprovided will not be NULL, and so we are guaranteed to not fall forthat here.
Let's have one more.
There is an old interview question in C++ thatgoes like: "implement the atoi STL function (the function that gets achar* 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" willproduce 124 as a result, meaning – coming across a non-number char willnot break the loop.
To look really bad, the same code could be writen in C# like this:
public static int atoi(string str) To look smarter in a C# interview, you might wanna consider writing it like so: public static int atoi(string str) if ('-'==c[0]){sign=-1; x=1;} for(;x < l;x++) return TheNumber*sign; }
{
return int.Parse(str);
}
{
int sign=1;
int TheNumber=0;
int tmp=0;
int x=0;
int l=(str.Length)-1;
char[] c=str.ToCharArray();
{
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
}
}
Pretty much the same ha?
Conclusion:
If you like C++ and find the string jargon annoying while moving to C#, use ToCharArray.












No comments yet... Be the first to leave a reply!