C Puzzles

yet another place for C puzzles

Wednesday, December 20, 2006

 

rotated sub string

Given a string s1 and a string s2, write a snippet to say whether s2 is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)


I hope this will work:
strcpy (s3,s1); //s3 = ABCD
strcat (s1,s3); // s1 = ABCDABCD
then check for strstr(s1,s2);

Tuesday, December 19, 2006

 

bit count

Write an efficient C program to count the number of bits set in an
unsigned integer.

i/p o/p
==== ===
0(00) 0
1(01) 1
2(10) 1
3(11) 2
..... ...

Ans:

int count_bit_set(unsigned int b)
{
int count =0;
while(b) {
b = b & (b-1);
count++;
}
return count;
}

Saturday, December 16, 2006

 

loop 20 times


int main()
{
int i, n = 20;
for (i = 0; i < n; i--)
printf("*");
return 0;
}

Change/add only one character and print '*' exactly 20 times.
(there are atleast 3 solutions to this problem :-)

I got TWO:
1.for (i = 0; i < n--)
2.for (i = 0; i + n; i--)

Monday, December 04, 2006

 

Good link

http://www.gowrikumar.com/programming/index.html


http://www.gowrikumar.com/

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]