Thursday, August 28, 2008

Where can I find free C/C++ compilers to download ?

Here is a list of free compilers (not exhaustive) that you can download and use.

For Windows


For *nix (Unix/Linux/Solaris etc.)


For Mac


Some compilers that require payment (some allow free downloads for trial periods):

Other lists of C/C++ Compilers

How can we connect to database using C/C++ ?

There are no standard libraries in C or C++ for handling any DBMS. We can only connect and communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.

Connecting to Database using Turbo C/C++

If you have a 16 bit compiler like Turbo C/C++ for DOS, then you cannot connect or access any database as there are no libraries supporting that compiler. You will need the new version of Turbo C++ (ie. Turbo C++ 2006 for Win32) which provides libraries for many different databases such as MSAccess, MySQL, Oracle, InterBase etc. You can download the new Turbo C++ 2006 (Explorer Edition) for free from http://turboexplorer.com/cpp. The help and documentation for Turbo C++ 2006 contains examples for connecting and querying different databases.

DBMS Libraries for C


MySQL C API
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html

DBMS Libraries for C++


OTL
OTL is a platform independent database library. It covers the functionality of a whole database API with just a handful of concrete classes and several template PL/SQL (Oracle) table container classes. The OTL code gets expanded into direct database API function calls, so it provides ultimate performance, reliability and thread safety in multi-processor environments as well as traditional batch programs. It supports Oracle 7 (natively via OCI7), Oracle 8 (natively via OCI8), Oracle 8i (natively via OCI8i), Oracle 9i (natively via OCI9i), Oracle 10g (natively via OCI10g), DB2 (natively via DB2 CLI), ODBC 3.x as well as ODBC 2.5 compliant data sources in MS Windows and Unix (e.g. Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS, etc.). The list of supported database backends is constantly growing.

MySQL++
MySQL++ provides C++ wrapper classes for C API's of MySQL database. More information can be found on their website at:
http://tangentsoft.net/mysql++/

SQLAPI++ Library
SQLAP++ provides C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from: http://www.sqlapi.com/

Code examples

Here is an MSDN link on how to connect to an Oracle database using VC++:
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaroledbp/html/msdn_ole4orcl.asp

Example for connecting and querying an MySQL Database, courtesy: Delbrooks's post in C community thread.
/*The 2 lines of code include the proper files in order to be able to compile the project. They are:*/
#include
#include
/*The several lines are used to #define the database details such as the host, the username, the password, and the database to connect to. Instead of creating 4 char pointers, I use 4 #define's like so:*/
#define host "localhost"
#define username "db_username"
#define password "db_password"
#define database "db"
/*You will have to change the values in the quotes to match the values that fit you. */

/*The next line creates a pointer of type MYSQL to the active database connection:*/
MYSQL *conn;

/*This program, being as simple as it is, will contain only one user defined function, main() and the rest of the functions used will be from the mySQL++ API (application program interface).*/

/*The next lines are ones that we all know and love:*/
int main()
{
/*Followed by that, we tell mySQL that are are about to connect with the mysql_init() function.
We pass a value of NULL to it because for our purposes, we don't need to go into it any further:*/

conn = mysql_init(NULL);

/*The next step is to actually connect to the database. This is where the conn variable and the host,
username, password, and database #define's are used:*/

mysql_real_connect(conn,host,username,password,database,0,NULL,0);

/*The next 3 lines are code that define variables to get a result and row pointer from the database,
along with a variable to increment in the loop if there is more than one row returned by the query:*/

MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i;

/*Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:*/
mysql_query(conn,"SELECT * FROM users WHERE userid=1");

/*After querying the result, we need to store the result data in the variable res_set we defined earlier. This is done like so:*/
res_set = mysql_store_result(conn);

/*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
the users table of the database). If so, then you need to find how many rows are returned so that you can
loop through each one and print the result of each one like so:*/

unsigned int numrows = mysql_num_rows(res_set);

/*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
This is done like so:*/

while ((row = mysql_fetch_row(res_set)) != NULL)
{
/*However, looping through the number of rows returned is useless if you don't find the number of fields in each row
(in this case, they are all the same because the query comes from the same table, but, if the query were produced
on the fly [in the while loop], then the function mysql_num_fields() is used to find the number of fields). I hope that
makes sense. Here goes:*/

for (i=0; i&ltmysql_num_fields(res_set); i++)
{
/*And finally, after all this time, we print the stuff out using the plain old C standard function printf():*/
printf("%s\n",row[ i ] ! = NULL ? row : "NULL");
}
}
/*Last but certainly not least, we close our connection to the database. When there are multiple connects and many users
accessing the database at the same time, it is essential that you connect and disconnect as soon as possible, or you can
have disasterous results. The final lines of code disconnect from the database and then exit the main() function:*/

mysql_close(conn);
return 0;
}


Below is an example code on how to connect to Sybase using C on UNIX flavors:
/* Anyone who understands this code, please edit this page and add comments*/
#include
#include
#include
int main()
{
DBPROCESS *dbproc;
LOGINREC *login;
DBCHAR name1[15];
DBINT no1;
RETCODE ret_code;
fflush(stdout);
dbinit();
login=dblogin();
DBSETLUSER(login,);
DBSETLPWD(login,);
dbproc=dbopen(login,NULL);
dbcmd(dbproc,"select * from table");
dbsqlexec(dbproc);
return 0;
}

What are the differences between structure and union ?

The main difference between a structure and union is in allocation of memory. In a structure, every member will be allocated its own memory space. Whereas for a union, the total memory allocated will be the memory required to accommodate the largest member. Suppose, a structure and union are declared as given below :

struct tmpStruct {
char name[20];
int id;
float f;
}varStruct;

union tmpUnion {
char name[20];
int id;
float f;
}varUnion;


Here the variable 'varStruct' will be allocated with total number of bytes required by its members, along with padding bits if it is required (ie. sizeof(name) + sizeof(id) + sizeof(f) + Padding). So here each member will be having its own memory location. On the other hand, the variable 'varUnion' will be allocated only with the largest member of it(ie. sizeof(name) + Padding). As a result, for a union only the member which was last assigned can be accessed. For example, the following code may give undefined results:
varUnion.id = 50;
varUnion.f = 100.00f;
printf("%d\n", varUnion.id); // Gives undefined result as last assigned member was varUnion.f.

While the same code works fine for a structure
varStruct.id = 50;
varStruct.f = 100.00f;
printf("%d\n", varStruct.id); // Prints 50

What are far, huge and near qualified pointers?

far, huge and near are non-standard qualifiers that were only used in 16 bit compilers like Turbo C/C++(DOS versions). A near qualified pointer is 16 bits in size and can access data within the 64kb data segment, while far and huge pointers can access data in other segments as well. A far pointer is 32 bits in size, of which the first 16 bits is for the segment address and the other 16 bits is for offset address. A huge pointer is normalized pointer and so its address can range beyond one segment.

These qualifiers are not available in the new 32 and 64 bit compilers (like VC++ and gcc) because they use flat memory model where memory is not divided into different segments, so these compilers do not have or require such qualifiers.

Can you write a code which compiles in C but not in C++ ?

This question puzzles many who believe that C++ is a superset of C language, and all C language code is valid in C++. But it is not true and you should read the differences between C and C++ to know how they are different.


Below are few examples which compiles in C and would fail to compile in C++:
Example 1:
#include
int main()
{
int class;
class=10;
printf("%d",class);
}

This will not compile under C++ because class is a keyword, but will compile in C without any problems. Similarly any keywords which are specific to C++ such as public, private, virtual, friend etc can be used as identifiers in C, but not in C++.

Example 2:
int fun()
{
//some code
}
int main()
{
fun(10,20);
}

This will fail to compile in C++, because in C++, the declaration of a function with no argument list is equivalent to declaring it as function with void parameter list. But in C language, it means a function with unspecified number of arguments and we can pass any number of arguments to this function in C.

Example 3:
#include
int main()
{
int *array=malloc(sizeof(int)*100);
}

This is valid in C because a pointer of type void* can be assigned to any other pointer without cast, but this is not in valid C++ because will have to give an explicit cast to it as in:
int *array=(int*)malloc(sizeof(int)*100);


Example 4:
#include
int main()
{
static int i=5;
if(i>0)
printf("%d\n",i);
else
{
i--;
main();
return 0;
}
}


This code is valid in C because recursion of main function is legal in C language whereas it is illegal in C++ language.

What are the differences between C and C++ ?

Many people believe that C++ is a superset of C, and even few books happen to say so. But C++ is different from C in many ways, and it is in fact an incomplete superset of C. There are certain features in C which are not in C++ and in some cases, valid code constructs in C may to fail to compile in C++, and in few other instances, the C code is executes differently when compiled with C++ compiler. Here is an excellent article by By David R. Tribble describing all the incompatible and differences between C and C++. Its very helpful article for those who code in both C and C++ languages.

Program to print its own source code as output

A Program which reproduces its own source code is called as a quine.
There are lots of ways to do this, here are some of them:

  • main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}

main(){char*a="main(){char*a=%c%s%c;int b='%c';printf(a,b,a,b,b);}";int b='"';printf(a

What is wrong with using void main()? Where does the returned value from main() go?

The main() function has to return an integer value which indicates the exit status of the program. The C and C++ standards only allow declarations for main function in the below forms:

  • int main(void) { /* ... */ }

  • int main(int argc, char *argv[ ]) { /* ... */ }

Or any other form which is equivalent of the above two, such as int main() and int main(int argc, char **argv).

So, declaring it as void main() or in any other form would make your program non-standard and non-portable. Some good compilers like gcc will issue a diagnostic and fail to compile when main function is improperly declared.

Where does the returned value from main() go?
The returned value from the main() goes to the host (ie. usually your operating system) which called your program. It indicates the exit status of the program to the host. Returning 0 means the program execution was successful, and returning any other value gives implementation defined status. The macro constants EXIT_SUCCESS and EXIT_FAILURE defined in can be used to indicate the programs termination status of success and failure respectively.

In Unix-like systems, the returned value of the program is stored in a shell variable $?. So, after the execution of your program, issuing the command 'echo $?' would give you the returned value from main.

References

Please don't void my main()!
void main(void) - the Wrong Thing
comp.lang.c FAQ: Can I declare main as void, to shut off these annoying "main returns no value" messages?

How to print 1 to n(a user defined value) without using any kind of loops or recursion ?

For C language, we can do this using setjmp and longjmp as given in the below code. Again there is an implicit loop, but no explicit looping construct or recursion used.

#include
#include

static jmp_buf jmpbuf;
static int val = 1, n;

void printvalue(void)
{
printf("%d\n",val++);
longjmp(jmpbuf,val);
}

int main()
{
printf("Enter the N value:");
scanf("%d",&n);
if (setjmp(jmpbuf) > n)
return 0;
else
printvalue();
}

Explanation

The first call to setjmp saves the current environment state in jmpbuf, and returns 0. So, if n is non-zero, the printvalue() is called. In printvalue function, the value of val is printed, and longjmp function is called, which returns the control back to main where the last setjmp was called. The setjmp macro on second and proceeding calls returns the val argument of longjump. This routine continues until the val becomes greater than n.

How to find greatest of two/three/four numbers without using relational operators ?

Finding greatest of two numbers:

  • int maxof2(int a,int b)
    {
    return (a + b + abs(a - b))/2;
    }


  • /****************************************************
    Purpuse : Evaluate the bigger one of two integers.
    Author : ALNG
    Date : 2003-03-11
    Original : http://search.csdn.net/Expert/topic/1515/1515035.xml
    **************************************************/


    inline int signof(int i)
    {
    return unsigned(i) >> (sizeof (int) * 8 - 1);
    }

    int max(int a, int b)
    {
    int p[2];
    p[0] = a;
    p[1] = b;

    return p[signof(a - b)];
    }

Function for finding greatest of three numbers:
int maxof3(int a, int b, int c)
{
return (a + b + c * 2 + abs(a - b) + abs(a + b - c * 2 + abs(a - b))) / 4;
}


Function for finding greatest of four numbers:
int maxof4(int a, int b, int c, int d)
{
return (a + b + c + d + abs (b - a) + abs(d - c) + abs(a + b - c - d + abs( b - a) - abs(d - c)))/ 4;
}



Warning: These functions might invoke undefined behavior when it overflows/underflows the integer value in the expression, so better not use these methods in actual applications, just use the relational/comparison operators.

How to find whether the given number is a power of 2 in single a single step (without using loops) ?

Ok this is another often repeated question. But unlike the others, this has a legitimate solution.

Before I show you the code for this, I would like to explain a few things first. We are required to find if a number is a power of 2 in just one step. A number would be a power of two if there is exactly one bit that is equal to 1 in its bit pattern (even 1 is a power of 2. 2 raised to the power zero is 1). Now we could use the method that we used to count the number of bits that are 1 in a given number and check if it is equal to 1. However that approach needs many steps and hence wont suffice here.

In a number which is an exact power of 2 only 1 bit is set and all others are zero. Let the position of this 1 bit be MSB. Mathematics rules for binary numbers tells us that if we subtract 1 from this number then the number that we would get would have all its bit starting from the bit position MSB+1 set to 1. For example if the given number num is 8(00001000) then num-1 would be 7 (00000111). Now we notice that these two bit patterns dont have a 1 in the same bit position. Further observation suggests that if we bitwise and (&) both these numbers we would get zero.

It can also be proved that num&(num-1) would be zero only if num is a power of 2. I leave that as an exercise. If you are'nt able to figure out why it is so, then just leave a comment here and I will explain that part too.

Now here is the code:

bool isPowerOf2(int num){
return ((num>0) && (num & (num-1))==0);
}

How to find whether a given number is even or odd without using % (modulus) operator ?

A newbie question. Here is a simple C++ solution.

bool isOdd(int num){
return num&1?true:false;
}


If the number is odd then it's least significant bit (LSB) is set i.e. it is 1. If it is even then it is 0. When you bitwise and (&) this number with 1, the result would be either 1 if the number is odd or zero if it is even.

As rightly pointed out by SpS, the same code can be implemented in C (following C99 standard) as

#include
bool isOdd(int num){
return (num&1)?true:false;
}


Here stdbool.h defines the macros bool, true and false. The macro bool is defined to be _Bool (the boolean type according to C99 standard). true is a macro which expands to the decimal constant 1 and false is a macro which expands to decimal constant 0.

For C89 sticklers

int isOdd(int num){
return (num&1);
}


You can use the returned value to determine if the number is odd or not. If the value returned is 0, the number is even. It is odd otherwise.

How to add two numbers without using arithmetic operators ?

Here is how you can add two numbers without using any arithmetic operators. The code uses bitwise operators. This code pretty much implements a full adder which adds three bits and returns two bits ( a carry and a sum).

A full adder circuit looks like this:
A full adder circuit

In the above diagram A and B are the bits to be added. C is the carry from a previous addition operator if any. S is the sum of the two bits minus the carry and C1 is the new carry which is to be used for further operations.

The truth table for a full adder would look like this:

C A B S C1
0 0 0 0 0
1 0 0 1 0
0 1 0 1 0
1 1 0 0 1
0 0 1 1 0
1 0 1 0 1
0 1 1 0 1
1 1 1 1 1



//This code doesn't include any overflow error checking mechanism. It is not much difficult to implement one however.
#include
int main(){
unsigned num1,num2,result=0,i=0,temp=0;
printf("Enter the two numbers:\n");
scanf("%d",&num1);
scanf("%d",&num2);
for (i= ~0; i; i>>= 1){ //ensure that the body of the loop is executed 8*sizeof(unsigned) times where 8 is the number of bits in a byte. The number of bits in a byte can be more than 8 on some machines.
temp<<= 1;
temp|= (num1^num2^result)&1;
result= ((num1|num2)&result|num1&num2)&1;//to understand this line and the one above take a look at the full adder circuit above
//here temp is the equivalent of S1 in the full adder circuit above and result is the equivalent of C1
num1>>= 1;
num2>>= 1;
}
//the bit order in temp would be in the reverse order. the following code snippet reverses the order
for (i= ~0, result= ~i; i; i>>= 1){
result<<= 1;
result|= temp&1;
temp>>= 1;
}
printf("Sum: %d",result);
return 0;
}


Here is another elegant solution that uses recursion. Uses the same algorithm as the previous code snippet. A simple value substitution would show you how it works.

#include
int add(int a, int b){
if (!a) return b;
else
return add((a & b) << 1, a ^ b);
}

int main(){
printf("Enter the two numbers: \n");
int a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
}


Below is one more way to add two numbers without the arithmetic operators. This method makes use of the asterisk(*) field width specifier in the printf function and use the returned value from printf. Here the printf statement is given with field width operator in format, and the two numbers are given as argument for field widths, hence we are printing a total width of two given integer arguments. The printf function returns the number of characters printed, and here the total number of characters printed will be sum of a and b. The third and fifth arguments are zero length strings because it only needs to print the total width of two integers.
Note that this method only works for unsigned integers.

unsigned add(unsigned a, unsigned b)
{
return printf("%*s%*s", a, "", b,"");
}

How to swap two numbers without using temporary variable ?

This one would figure in anyone's list of elite questions in Orkut programming communities that are guaranteed a minimum of 50 responses everytime someone raises it, if at all anyone were to be jobless enough to compile one such list. This question is often asked in many interviews in India and so obviously many would be interested in knowing the answer.

And it would come as a rude jolt to many when confronted and told that there is no standard conformant way of doing this which would apply equally well to all built-in types.

I would like to discuss all the solutions that have been offered so far.

a=a+b;
b=a-b;
a=a-b;


The above method wont work because it doesn't take into account overflow errors. Suppose for example if we had to swap two signed numbers 1 and 32767 on a system where int is 16 bits long. On such a system, the maximum value an int can store (INT_MAX) would typically be 32767.

So when the first step a+b is executed the result would be 32768, which would be larger than the maximum value that can be stored in an int variable. Consequently this value cant be stored in a and hence an overflow occurs. What happens after this is undefined. No matter what happens under the hood, the result is bound to be undefined and hence this method fails whenever an overflow occurs.

a=a*b;
b=a/b;
a=a/b;


This method would fail for the same reason the previous code snippet would fail. Here a*b can overflow. This has more problems as well. a/b can overflow too. This method would also fail if b were zero, as it would cause divide by zero error. So this is another worthless suggestion.

a=a^b;
b=b^a;
a=a^b;


The method above wont work for floating point values. Bitwise operators are not defined for floating points and hence this solution can be used only when the numbers to be swapped are not floating points. Usage of this method on variables other than int (char, long, short) would result in Undefined Behavior(UB). In all the three methods above, the compiler implicitly uses temporary values to store the result from the mathematical operations and this temporary value is then copied into the l-value. So technically we are using temporary values here too. But let us proceed assuming that the question here is to swap two variables without defining any temporary variable in the user code.

a^=b^=a^=b;


a=a+b-b=a;


The two code snippets shown above impresses almost every newbie the first time they see it. They would leave no stone unturned in praising the author of this code. Little do they realize that this code is not guaranteed to produce the same result on every system they test it on.

Here we are entering the realm of Undefined Behavior often abbreviated as UB.

There are three kinds of operators: unary, binary and ternary. Unary operators are those which operate on one and only one operand. Unary+, Unary-,dereferencing operator* are some examples. Binary operators are those which operate on two operands. Addition (+), Subtraction (-), Logical Or (||) are a few examples of binary operators. There is only one ternary operator in C++ viz ?:.

When the compiler sees an expression, it breaks them into smaller units depending on a table called the operator precedence table. The operators at the top of the table are the ones executed first followed by other operators in the decreasing order of precedence.

Now each of these operators can operate on one, two or three operands. These operands can themselves be expressions.

For example : int a=(b+c)*(d+e);

Here the operator * has two operands. The former is a simple expression (b+c) and the latter is another simple expression (d+e).

Now the C++ standard doesn't place any condition on the order in which these expressions must be evaluated. A normal human being would first calculate (b+c) and store it in a temporary variable. He would then calculate (d+e) and store it in another temporary variable. He would then retrieve these two temporary values and then multiply them and store the result in the variable a. But the standard lays down no such conditions or rules. It leaves the choice to the compilers. So the compilers can evaluate either (d+e) first or (b+c) first. Different compilers choose to do it differently.

For the simple expression given above this doesn't cause much confusion. The reader would probably be wondering as to why I am rambling on about it when evaluating (b+c) or (d+e) first doesn't change the result.

Surprise!!!

Consider this expression:

int i=1;
int temp=i++*(i-1);

Here the operands of the multiplicative operator * are i++ and i-1;

Now as mentioned before the compiler can evaluate either i++ first or (i-1) first. Let us see what happens when i++ is evaluated first. After i++ is evaluated the value that would be returned would be the existing value i.e. 1 and i would now be 2. After this i-1 is evaluated which would be 1. So i++*(i-1) would be 1*1 which is equal to 1. So temp would be 1 now.

Now let us see what happens when (i-1) is evaluated first. Since i is 1, i-1 becomes zero. Irrespective of what i++ is now, i++*(i-1) would be zero. So temp is initialized to zero here.

Do you realize the mistake? The result here depends on the order of evaluation and hence is unpredictable. It is unreasonable to expect all compilers to follow the same order of evaluation. Hence temp will be initialized to 1 on some systems and it will be initialized to zero on some other systems. This is Undefined Behavior and it is more often than not better to avoid coding practices like these.

Now this brings me to the discussion of sequence points. I have tried hard and exhausted myself in the process of trying to convince people that they should pay special attention to this topic. Please check C-faq and Wikipedia article for more info on sequence points.

After reading those topics some users confuse themselves between operator precedence and sequence points. Sequence points has got nothing to do with operator precedence. This requires more explanation.

When a compiler sees an expression with many operators, it breaks the expression into many logical and simpler units. How it does this depends on operator precedence.

For example when the compiler sees an expression like a+b*c+d, here the two operands are binary multiplicative operator * and binary addition operator +. * has higher precedence.

So the first step would be to choose * as the point where the expression can be split into simpler units. So the expression would now become a+(b*c)+d.

This expression is evaluated from left to right as + has left to right associativity. So the expression would become ( a + (b*c) ) + d

Now assuming a and d are simple expressions themselves. In this case consider the first binary addition operator.

Its operands are d and ( a + (b*c) ). Now as I mentioned before, the compiler can evaluate either d first or it can evaluate ( a + (b*c) ) first.

Now if it chooses to evaluate ( a+ (b*c) ) first, it can further choose to evaluate (b*c) first or a first. And further if b and c were expressions themselves, then we cant be sure about which amongst b or c would be evaluated first. The standard doesn't guarantee much here.

So the expression a+b*c+d which appears so simple to the naked eye has so many complications when we enter the realm of compilers and it is always better not to assume anything.

Hope that clears things a bit.

Now coming back to the original discussion. Why do those two code snippets lead to UB?

For precisely the same reason (i++)*(i-1) leads to UB. In the first code snippet there are many XOR operations operating on the two variables a and b. The author of the code ends up modifying the variables a and b more than once between two sequence points which results in UB.

In the second code snippet, the author assumes that b=a would be evaluated first before all other expressions. It may or may not be evaluated first. The results would be different for both the cases and hence leads to be UB again.

Now to summarize, there is no known way to swap two variables without using a third variable that would apply equally well to all built-in types.

How to printf "Hello World" without using a semicolon

How to use printf function without semicolon?



There are several ways to do this, below are some of the most popular ones:

  • if(printf("Hello, World!\n")) { }

  • while(!printf("Hello, World!\n")) { }

  • switch(printf("Hello, World!\n")) { }

This exercise has no particular use, but it keeps puzzling the newbies, and most of the companies in India happen to ask this question in interviews.
Explanation
The if, while and switch statements take a scalar type value as expression. Since printf function returns an integer (scalar type) value indicating the number of characters printed successfuly, it can be given as an expression to these statements and do away without the semicolon.

Wednesday, April 16, 2008

Write a Program to Reverse a String

There are a number of ways one can reverse strings. Here are a few of them. These should be enough to impress the interviewer! The methods span from recursive to non-recursive (iterative).

Also note that there is a similar question about reversing the words in a sentence, but still keeping the words in place. That is


I am a good boy


would become


boy good a am I


This is dealt with in another question. Here I only concentrate on reversing strings. That is


I am a good boy


would become


yob doog a ma I



Here are some sample C programs to do the same


Method1 (Recursive)


#include

static char str[]="STRING TO REVERSE";

int main(int argc, char *argv)
{
printf("\nOriginal string : [%s]", str);

// Call the recursion function
reverse(0);

printf("\nReversed string : [%s]", str);
return(0);
}

int reverse(int pos)
{
// Here I am calculating strlen(str) everytime.
// This can be avoided by doing this computation
// earlier and storing it somewhere for later use.

if(pos<(strlen(str)/2)) { char ch; // Swap str[pos] and str[strlen(str)-pos-1] ch = str[pos]; str[pos]=str[strlen(str)-pos-1]; str[strlen(str)-pos-1]=ch; // Now recurse! reverse(pos+1); } } Method2 #include #include #include void ReverseStr ( char *buff, int start, int end )
{
char tmp ;

if ( start >= end )
{
printf ( "\n%s\n", buff );
return;
}

tmp = *(buff + start);
*(buff + start) = *(buff + end);
*(buff + end) = tmp ;

ReverseStr (buff, ++start, --end );
}


int main()
{
char buffer[]="This is Test";
ReverseStr(buffer,0,strlen(buffer)-1);
return 0;
}




Method3


public static String reverse(String s)
{
int N = s.length();
if (N <= 1) return s; String left = s.substring(0, N/2); String right = s.substring(N/2, N);
return reverse(right) + reverse(left);
}





Method4

for(int i = 0, j = reversed.Length - 1; i < j; i++, j--)
{
char temp = reversed[i];
reversed[i] = reversed[j];
reversed[j] = temp;
}
return new String(reversed);





Method5


public static String reverse(String s)
{
int N = s.length();
String reverse = "";
for (int i = 0; i < N; i++)
reverse = s.charAt(i) + reverse;
return reverse;
}





Method6

public static String reverse(String s)
{
int N = s.length();
char[] a = new char[N];
for (int i = 0; i < N; i++)
a[i] = s.charAt(N-i-1);
String reverse = new String(a);
return reverse;
}