Sunday, November 1, 2009

Why Python kicks C right in the nuts

Program to reverse a string in C:

#include

int main()
{
char s[3] = {'a', 'b', 'c'};
int i, temp;
for(i = 0; i < strlen(s)/2; i+=1)
{
temp = s[strlen(s) - i - 1];
s[strlen(s) - i - 1] = s[i];
s[i] = temp;
}
for(i = 0; i < strlen(s); i+=1)
{
printf(s[i]);
}
return 0;
}


The corresponding program in Python:

s = 'abc'
s = s[::-1]
print s

Enough said.

P.S. : Sorry about the intendation.