Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Manipulating Strings The Way C/C++ Programmers Use To
By Sagiv Hadaya

I have been asked a lot lately, how come it's so different in C# to play around with strings? How come we have to do so many manipulations. We used to do this using recursions. While recursions are fine, they are a main source for flaws and glitches, and I 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) 
{ 
	/*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, it's hard for some of us to keep track of what is 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 normally everything we did 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 promised by .NET that the string x that is provided will not be NULL, and so we are guaranteed to not fall for that here.

Let's have one more.

There is an old interview question in C++ that goes like: "implement the 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 - coming across a non-number char will not break the loop.

To look really bad, 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.