int palindrom(const char *str)
{
int len;
int i;
len = strlen(str);
len--;
for(i=0; i < len;i++,len--)
if(str[i]!=str[len])
return 1;//not a palindrome
return 0;//a palindrome
}
A place where you can share ideas to do a better programming. A better code. A better world :)
Saturday, July 26, 2008
To reverse a string.
void rev(char *str)
{
int len;
int i;
char temp;
len = strlen(str)-1;
for(i=0; i < len; i++,len--)
{
temp=str[i];
str[i]=str[len];
str[len]=temp;
}
}
{
int len;
int i;
char temp;
len = strlen(str)-1;
for(i=0; i < len; i++,len--)
{
temp=str[i];
str[i]=str[len];
str[len]=temp;
}
}
A program to move a circle in a box.
#include
#include
#include
#include
#define DELAY 4
#define RADIUS 10
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int signx=1, signy=1;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw the circle and move it continously */
rectangle(0,0,getmaxx(), getmaxy());
while(!kbhit())
{
setcolor(WHITE);
circle(midx, midy, RADIUS);
delay(DELAY);
setcolor(BLACK);
circle(midx, midy, RADIUS);
midx=midx+signx*1;
midy=midy+signy*1;
if( (midx+RADIUS+1>= getmaxx()) || (midx-RADIUS-1 <= 0) )
signx=-signx;
if( (midy+RADIUS+1>=getmaxy()) || (midy-RADIUS-1<= 0))
signy=-signy;
}
/* clean up */
getch();
closegraph();
return 0;
}
#include
#include
#include
#define DELAY 4
#define RADIUS 10
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int signx=1, signy=1;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw the circle and move it continously */
rectangle(0,0,getmaxx(), getmaxy());
while(!kbhit())
{
setcolor(WHITE);
circle(midx, midy, RADIUS);
delay(DELAY);
setcolor(BLACK);
circle(midx, midy, RADIUS);
midx=midx+signx*1;
midy=midy+signy*1;
if( (midx+RADIUS+1>= getmaxx()) || (midx-RADIUS-1 <= 0) )
signx=-signx;
if( (midy+RADIUS+1>=getmaxy()) || (midy-RADIUS-1<= 0))
signy=-signy;
}
/* clean up */
getch();
closegraph();
return 0;
}
Sunday, July 20, 2008
Friday, July 11, 2008
to find whether a number is in power of 2 or not.
/*solution in C language*/
int powerof2(int num) /*return 1 if number is in power of 2 otherwise 0*/
{
return !(num & num-1);
}
int powerof2(int num) /*return 1 if number is in power of 2 otherwise 0*/
{
return !(num & num-1);
}
Find the sum of a series. S = 1-3+5-7+9 ... n
/*in c language */
int s=0;
int sign=1;
int i;
for(i=1; i<=n; i+=2)
{
s = s+ sign*n;
sign = -sign;
}
/*s will contain the sum of the series*/
int s=0;
int sign=1;
int i;
for(i=1; i<=n; i+=2)
{
s = s+ sign*n;
sign = -sign;
}
/*s will contain the sum of the series*/
Subscribe to:
Posts (Atom)