Tuesday, March 17, 2009

Working with std::string::substring() method

Though it is a very simple function, but sometimes people(me too) make a mistake when coding thing up. For example we have a std::string strHello

int nPositoinOfSemiColon = strHello.find(";", 0);

find method will find the fist parameter ";" in strHello, starting from second parameter i.e. 0.
Now the nPositoinOfSemiColon will contain either string::npos which means the string provided to find method was not found in strHello. Or in our case, 5 which is the location of ";".

strHello.substr(0, nPositoinOfSemiColon );

will return "Hello", i.e. 5 characters starting from location 0 in strHello.
Now consider

int nPositionOfSecondSemiColon = strHello.find(";", nPositionOfSemiColon);

Well... writing following line of code will... wonder what...

strHello.substr(nPositionOfSemiColon, nPositionOfSecondSemiColon);

CAUSE AN ERROR!!! FOLKS!!! ERR GRR ERROOORRR... a vicious cruel ugly error... heh heh heh...
Poor baby programmers will asK "But why Uncle why???"
Because, of the values in variables at this time are...

nPositionOfSemiColon = 5
AND
nPositionOfSecondSemiColon = 11
That means
strHello.substr(5, 11);

i.e. extract 11 characters starting from location 5 in strHello.
Whereas, 11 is total length of strHello...

The right way to go is

strHello.substr(nPositionOfSemiColon,
nPositionOfSecondSemiColon - nPositionOfSemiColon);

That means
strHello.substr(5, 6); // That means ";WORLD"

Cheers folks... have fun... remember... coding is a noble pursuit... so just keep hitting it hard... savor the wonders and joys of turning real world into programming language codes, and bear the tensions with enough tolerance...

More C/C++ Interview Questions

I was keeping this post on hold for almost a month, so that I will post the questions along with the answers... but due to some reasons, I was unable to post the answers...
Will do it some time later in future...
For the time being, the C/C++ interview questions are given below...
If anybody finds out an answer, feel free to write it in comments...

1. What is the difference between a pointer and array?
2. Is it possible to find out the size of an array?
3. What is NULL?
4. How can we find if a pointer contains a valid value or not?
5. We have a circular linked list, and its tail -> next is not pointing to List -> Head. How can we diagnose such an error condition?

Monday, March 9, 2009

A tricky error, when using std::string with *printf funtions

All of us know what this code (listing 1)will do...

char * szName = "Ex";

printf("%s", szName );

Will anybody tell what will be the result of these lines(listing 2)...


std::string strName = "Zed";
printf("%s", strName);

Ladies and gentlemen... listing two will cause your program to crash. This is a very common mistake made by programming newbies... though an experienced pro may also do the same...

The right way to print the string is

std::string strName = "Zed";

printf("%s", strName.c_str());

such errors are very difficult to spot... Because when reading the code, one often unconsciously considers these lines are correct...

Now people will ask that why in the world will someone create such a ridiculous blend of C and C++... The answer is... I don't know why... But anybody is absolutely free to mix the two compatible languages into a cocktail... I myself did this... when I was new into the dark realm of C/C++ programming... I had my own reasons... others will have their own...

note: *printf means any of the following funtion calls {printf, sprintf, fprintf}

Wednesday, March 4, 2009

Relevance of C/C++ in today's computing

A question mostly asked by fresh graduates is "C/C++ are still being used"? The answer is "Yes of course", C/C++ are still being widely used. Many open source projects are done using C/C++. Modern computing scene would be very different if C/C++ were not there... For example

1. Asterisk the open source PBX is written using plain old C language.
2. Maya, a top notch 3D animation suite is written using C++.
3. Postgres, leading open source DBMS which competes to Oracle is done using C.

C is considered the language of choice for writing Operating Systems. All Linux based operating systems are written using C. Microsoft Windows is written using C/C++. They (folks @ Redmond) tried to write Windows Vista using C#, but failed miserably.
C is known as "Mother of all programming languages". C/C++ are primarily used to write mission critical software, when we need high speed and smaller memory foot prints.I agree that we are living in an internet age, languages lis PHP/ASP.Net/Ruby on Rails are everywhere these days. But does anybody think, what is the engine powering these languages? The web-server which executes PHP code, the browser which displays CSS/HTML and dances around with AJAX, Adobe's Flash things, and many more are all done using C/C++.
Sometimes I think, I will do a detailed workshop titled "The Real Power of C/C++". I'm pretty ambitious in this regards, and I'll surely tell the world about "the happening"...
c-plus-plus