Tuesday, 16 September 2014

Program to reverse the string in C

/*
         Program to reverse a string like - "program to reverse the string in c" to convert this string into "c in string the reverse to program"
         Created By: Pirate
*/
#include<string.h>
void main(){
           int i,j,k;
           char s[100];
           printf("Enter a string\n");
           gets(s);
           k=strlen(s);
           for(i=k;i>=0;i--){
                    if(s[i]==' '){
                               for(j=i+1;j<k;j++){
                                         printf("%c",s[j]);
                               }
                               printf(" ");
                               k=i;
                    }
          }
          for(i=0;i<k;i++)
          printf("%c",s[i]);
}


Output


Thursday, 20 March 2014

Program to print Elements of a Matrix in Spiral Order in c

Hi friends,
               Today I'm here with a new program that is print elements of a matrix in spiral order. We have to start from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown below.

                                          Input matrix 4x4

                                            1    2    3    4
                                            5    6    7    8
                                           9   10   11  12
                                           13 14   15  16

and the output of the program is ,
                                           1 → 2  → 3 → 4
                                                                    ↓
                                           5 → 6  → 7      8
                                           ↑               ↓      ↓
                                           9     10 ←11   12
                                           ↑                       ↓
                                          13←14←15←16

Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

/*
   Print Matrix in Spiral Order
   Created by: Pirate
*/

#include<stdio.h>
#include<conio.h>
int main()
{
int a[100][100],i=0,j,k,r,c,i1=0,j1=1,p,count=0;
printf("Enter the size of matrix\t");
scanf("%d%d",&r,&c);
printf("Enter the %dX%d matrix",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
p=c*r-1;
j=0;
while(1)
{
for(i=i1;i<c;i++)
{
printf("%d ",a[j][i]);
count++;
}
for(j=j1;j<r;j++)
{
printf("%d ",a[j][i-1]);
count++;
}
for(i=r-2;i>=i1;i--)
{
printf("%d ",a[j-1][i]);
count++;
}
if(count>p-1)
break;
for(i=j-2;i>i1;i--)
{
printf("%d ",a[i][i1]);
count++;
}
i1++;
j1++;
c--;
r--;
p--;
j=i1;
}
        getch();
        return 0;
}

Output:


Enjoy :)



           

Thursday, 6 March 2014

Different ways to swap values

Hi friends,
                Today I'm going to talk on swapping concept. You all know how to swap but which is the fastest way to swap two variables values. What is swapping? It's mean to exchange values of one variable to another variable. I'm going to describe here three method to swap two variable. In first method we'll use a temporary variable that is used to store intermediate result. In second method we'll use addition and subtraction but no other variable and in the last method we use bitwise XOR operator. Lets take two variable named A,B.

A=10
B=20

after swapping

A=20
B=10

Method:1 (Swapping using temporary variable)
Here we have to take another variable temp. It is the simplest method to swapping values between variables.

temp=A;
A=B;
B=temp;

Method:2 (Swapping with Addition and Subtraction)
In this method we don't use another temporary variable. It can only swap numeric variable values and if the value stored in the variable is little less from its range then it will not work properly and will give incorrect answer. 

A=A+B;
B=A-B;
A=A-B;

Method 3 (Swapping using XOR bitwise operator)
It is the fastest method to swap two variables but it's not working on floating type variables and gives correct output on character and integer type variables.

A=A^B;
B=A^B;
A=A^B;

Here is the full program,

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,temp;
clrscr();
printf("Enter two no");
scanf("%d %d",&a,&b);
printf("Swapping using temporary variable\n");
temp=a;
a=b;
b=temp;
printf("A=%d\t%d",a,b);
printf("\nSwapping with Addition and Substraction\n");
a=a+b;
b=a-b;
a=a-b;
printf("A=%d\tB=%d",a,b);
printf("\n Swapping using XOR bitwise operator\n");
a=a^b;
b=a^b;
a=a^b;
printf("A=%d\tB=%d",a,b);
getch();
        return 0;
}

Output:


Enjoy :)

Saturday, 18 January 2014

Amazing google game 2

Hello friends,
                  I have posted about a google game that is enough good. Today i'm going to tell you about another google games. Actually these game are used by google as a doodle at the time of olympic in 2012. Here four games are listed. You can play these games and share your score and compete with your friends. If you like them please comment below.

                The first game is Basketball and this is my favorite game. In this, they have given you 24 seconds and in these 24 seconds how more you baskets your score will increase. Another game named Slalom Canoe. In this, you have to finish the race with your Canoe in minimum seconds using your keyboard arrow keys. Your score will have determined according to time that is taken by you to finishing the race. Third one is Soccer in which you will have to secure your goal post and you have given three chances. The last one is Hurdles in which you have to complete the race by crossing obstacles. You can play these games by clicking their corresponding links.

(1) Basketball

Click Here




(2) Slalom Canoe

 Click Here






(3) Soccer

Click Here





(4) Hurdles

Click Here


Enjoy :)