Saturday, September 17

C program to find Size of a structure without using sizeof() operator

Write a program to find the size of following structure without using size of operator

struct ABC
 {
 int a;
 float b;
 char c;
 };



Hai Viewer, confused?? Hmm.. calculating the size of a datatype without using sizeof() operator seems to be weird. But pointers will help you to solve this problem.  First u have to initialize a null pointer on intended datatype .You know that in 'structure ' separate memory will be allocated to each element. So, on incrementing that pointer variable, the value of that pointer will be incremented by size of that structure . That is our Goal..!!!! :)
Since we have initialized pointer with 0 we simply need to print the current pointer value..!!



solution

#include<stdio.h>
struct  ABC
{
    int a;

    float b;
    char c;
};
int main()
{

    struct ABC *ptr=(struct ABC *)0;
    ptr++;
printf("%d",ptr);
getch();
return 1;
}



I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)





C program to Print a Non-Fibonacci series

Write a C program to generate non-fibonacci series to given Limit:

#include<stdio.h>
#include<conio.h>

main()
{
 int n,a,b,c,d,x;

 a=0;
 b=1;
 c=0;
 printf("Enter the upper range of the series:");
 scanf("%d",&n);

 while(c<=n)
 {
  c=a+b;
  a=b;
  b=c;
  d=a+b;

  for(x=c+1;x<d;x++)
  {
   if(x<=n)
    printf("%d",x);
   else
    break;
  }
 }
 getch();
}


I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)

C program to Reverse a string using recursion

Write a program to reverse any string using recursion (Maximum 15 steps)

Eg: Input   :  Bjarne Stroustrup developed C++
     Output  : ++C depoleved purtsuortS enrajB


This is one of the common question asked in technical contests and interviews.!! it checks ur knowledge about recursion and string. We recursively calls itself until encountering endl character. This fragment plays the vital role: 

{
if (current character != endl) process( next caharacter);
print(current character); 
}


My code:

#include<stdio.h>
char line[20];
void print(int i)
{
     if(line[i]!='\0')
     print(i+1);

     printf("%c",line[i]);
}
main()
{
      printf("Enter a  string..:   ");
      gets(line);
      print(0);
      getch();
}
 




I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)

Coding Contest- Matrix multiplication

PROBLEM : Write a program to multiply two 3 x 3 matrices where resulting matrix should produce
  • If cell element is even '0'
  • If cell element is odd '1'
eg: If resulting matrix of multiplication is

My code:


#include<stdio.h>
main()
{
      int a[3][3],b[3][3],c[3][3],d[3][3];
      int i,j,k;
      printf("Enter First matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
       printf("Enter Second matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&b[i][j]);
     
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      c[i][j]=0;
      for(k=0;k<3;k++)
      c[i][j]+=a[i][k]*b[k][j];
      }
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      if(c[i][j]%2)d[i][j]=1;
      else d[i][j]=0;
      }
      printf("\n Product is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",c[i][j]);
      printf("\n");
      }
       printf("\n Response is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",d[i][j]);
      printf("\n");
      }
     
      getch();
}
     
     

Friday, September 9

LEX Program to identify Keywords and convert it into uppercase


Guys, apply simple logic. Detect all keywords( This program deal with a small number of keywods for sake of simplicity).
On detecting any keyword in C, convert it into uppercasr letter using the funtion 'toupper' . Game over!!




%{#include<stdio.h>
int i;

%}keyword main|int|scanf|printf|if|else
%%

{keyword} {
 for(i=0;i<yyleng;i++)
 printf("%c",toupper(yytext[i]));
   }

%%

main()
{
yyin=fopen("num.c","r");
yylex();
}


int yywrap()
{
return 1;
}

OutputLet num.c contains following program fragment.

main()
{
int num;
scanf("%d",&num);
if(num%2)printf("Odd");
else printf("Even")
}


The output will be,

MAIN()
{
INT num;
SCANF("%d",&num);
IF(num%2)PRINTF("Odd");
ELSE PRINTF("Even")
}





LEX Program to eliminate all whitespaces

Hi Guys,
This lex program is aimed to eliminate all whitespaces from an input file. As u think, this is not at all a difficult job. We have to simply detect single blank ' ' or tab '\t' or a newline '\n' and delete them in output. That's all..!!!!



%{
#include<stdio.h>
%}

%%
[\n\t ' '] {};
%%
main()
{
yyin=fopen("myfile.txt","r");
yylex();

}
int yywrap()
{
return 1;
}

____________________________________________________________________
Look @ the example:

Basic datatypes in C are:
int   char  float   double

output will be

BasicdatatypesinCare:intcharfloatdouble




.

LEX Program to delete all comments


No Comments  :)


This lex program is intended to eliminate comments from a C program. This task is simple since in a C program, comments will be associated only with '//' or '/*...*/ only. So our aim is to detect the occurence of these characters and ignore subsequent comments.



%{
#include<stdio.h>
%}


%%

\/\/.* ;
\/\*(.*\n)*.*\*\/ ;

%%

main()
{
yyin=fopen("mypgm.c","r");
yylex();
}


int yywrap()
{
return 1;
}





\/\/.* ; eliminates single line coments. i.e., comments of the form ' //comment.. .. .. ;'
It simply look on '/'immediately followed by another '/' and ignore that line.

\/\*(.*\n)*.*\*\/ ; eliminates multiple comments. i.e, code fragment lies within /*...*/


Have  a look on this Example:

Consider mypgm.c

//This is a single comment;
This is not a comment;
/*But..
These are mulptiple comments
*/
Again, It is not a comment


The output will be,

This is not a comment;
Again, It is not a comment