Saturday, September 17

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

LEX Program to count the number of lines, words and letters


Howdy guys,

Lets have a look on how a Lex programs works using a simple example.

This sample programs counts the number of lines, words and characters in a text file.


 
Lex programming is not rocket science. You have to just apply an eight year old kid's logic. Yes, you read right. Only a school kid's logic.

LOGIC

Read each character from the text file :

  • Is it a capital letter in English? [A-Z] : increment capital letter count by 1.
  • Is it a small letter in English? [a-z] : increment small letter count by 1
  • Is it [0-9]? increment digit count by 1.
  • All other characters (like '!', '@','&') are counted as special characters
  • How to count the number of lines? we simply count the encounters of '\n' <newline> character.that's all!! 
  • To count the number of words we count white spaces and tab character(of course, newline characters too..)

 Alignment of a Lex program is very simple and it makes the logic more vivid.



counter.l
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%


 
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%





main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
 
int yywrap()
{
return(1);
}


Sample output



 Let the 'myfile.txt' contains this.

                         This is my 1st lex program!!!
                         Cheers!! It works!!:)

The output will be


This file contains..
2 lines
9 words
30 small letters
3 capital letters
1 digits
9 special characters
In total 43 characters.



Thursday, September 1

HOW TO INSTALL MATLAB in Windows 7 ???


MatLab is a software by which  you can write nice little programs  and produce pretty pictures and graphs!! It has already inbult functions based on C,C++ and Java etc. We can directly use them or we can develop our own. This language contain programming, graphical used interface, simulink and Animations,coming to electronics ...u have signal procesing, image processing, communications and other related topics directly in the MATLAB.

            In this post, let us have a look on how to install MatLab in Windows 7 !!

 Installation of Matlab requires two sections, one activation with License code and next activation with .dat file. The complete steps are as follows:
  1. Find  the SETUP file.( Usually inside matlab --> win32). 

2. Double click setup.exe

3.Generally two options will be provided:
                  - Install automatically using the internet.
                  - Install manually without using the internet.

4. Select to install manually without the Internet.



5. On clicking " Next " button, the installer will prompt you to enter the License key.


6.Enter the License key provided within the crack  ( .txt file)


7.Click "Next" --> Choose "Typical installation".


8. Installation begins.(usually it will take 10-15 minutes).


9. After this installation, some activation is needed.(This step makes little confusion for many people that reinstallaion being occuring here. Actually this time installer demands a .dat file for activation).




10. Select "continue"--> "Activate manually without the Internet".

11. installer will prompt you to enter the License file.


12. Click 'Browse' and provide path to the License file.


13. Usually it is available inside the folder "Crack" in the DVD).




14. Choosing the License File:: Users who gave the Key for 'standalone version' may use the file 'Lic_StandAlone.dat' and those gave the key for 'server edition' may give the License file 'Lic_server.dat'.




15. On selecting License file, installation get completed .



    

 Still have doubt about Matlab installation???  Click here for more details...