Tuesday, August 30, 2011

Check whether the processor is available or not

This program will check whether the processor is available or not..
#include<stdio.h>
#include<conio.h>
void main()
{
printf ("Checking if processor is available...");
if (system(NULL))
printf("Processor is available");
else
printf("Processor is not available");
getch();
}

Running a DOS command using C

To Run a DOS command using C we use the system() function present in process.h and stdlib.h
The prototype of the function is:
int system(const char *command);
On Success returns 0
On Error returns -1
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
printf("This program will execute the DOS command");
i=system("dir");
if(i==0)
printf("completed successfully");
else
printf("error in executing the command");
getch();
}
If you are using turboc3 and run this using Ctrl+F9 you will not see the error , to run this go to cmd prompt and then execute the exe file created after compiling the program form the DOS prompt.you will easily get the output..if you are having any problem...just mail me at milankmr2011@gmail.com

Reading a file line by line in C

This post will help you in reading a file line by line...
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fp=fopen("c:\milan.c","r");
clrscr();
   if(fp!=NULL)
   {
   char line[128];
   while(fgets(line,sizeof line,fp)!=NULL)
   {
   char *nwln=strchr(line,'\n');
   if(nwln!=NULL)
   *nwln='\0';
   printf("%s\n",line);
   }
   }
  getch();
}

Tuesday, August 23, 2011

sorting the strings

#include<conio.h>
#include<string.h>
void main()
{
char *names[]={"zzz","aaa","ccc","yyy"};
int i=0,j=0;
char *temp;
for(i=0;i<4;i++)
{
for(j=i;j<4-i-1;j++)
{
if(strcmp(names[j],names[j+1])>0)
{
temp=names[j];
names[j]=names[j+1];
names[j+1]=temp;
}
}
}

for(i=0;i<4;i++)
{
puts(names[i]);
}
getch();
}

String valid or not

#include<stdio.h>
#include<conio.h>
#define SIZE 100
struct stack
{
int top;
char items[SIZE];
};
void push(struct stack*,char);
char pop(struct stack*);
int empty(struct stack*);
void main()
{
struct stack s;
struct stack *ps;
char *str;
int valid=1;
ps=&s;
s.top=-1;
clrscr();
printf("enter a string to check its validity\n");
printf("String::");
scanf("%s",str);
printf("\nString is %s",str);
while(*str!='\0')
{
char ch;
char temp;
ch=*str;

//printf("\n character taken from string is %c",ch);

if(ch=='(' || ch=='{' || ch=='[')
push(ps,ch);
if(ch==')' || ch=='}' || ch==']')
{
if(empty(ps))
{
valid=0;
break;
}
else
{
temp=pop(ps);
if(temp=='('&& ch==')' || temp=='{'&& ch=='}' || temp=='['&& ch==']')
{
valid=1;
}
else
{valid=0;break;}
}
}
str++;
}//end while

if(!empty(ps))
valid=0;

if(valid)
printf("\nString is valid");
else
printf("\nString is not valid");

getch();
}//end main

void push(struct stack *ps,char ch)
{
ps->items[++(ps->top)]=ch;
}
char pop(struct stack *ps)
{
return(ps->items[(ps->top)--]);
}
int empty(struct stack *ps)
{
if(ps->top==-1)
return 1;
return 0;
}
output:
enter a string to check its validity
String::a+b(-2+3))
String is a+b(-2+3))
String is not valid

Sleep function in C

#include <dos.h>
#include <stdio.h>
#include<conio.h>
void main()
{
char s[]="milan";
int i;
for(i=0;i<5;i++)
{
printf("%c",s[i]);
sleep(1);
}
getch();
}

Breaking the string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s;
int len,i,j;
clrscr();
printf("enter a string:");
gets(s);
len=strlen(s);
for(i=0;i<len;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",*(s+j));
}
printf("\n");
}
getch();
}

Generating the Random number

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
 int i;
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
getch();
return 0;
}

busy process....

#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
    int m,kbhit(void);
    clrscr();
    while(!kbhit())
    {
        printf("\b\\");
        delay(100);
        printf("\b|");  
        delay(100);
        printf("\b/");
        delay(100);
        printf("\b-");
        delay(100);
    }
return;
}

left padding a digit

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n=7;
printf("%.10d",n);//padding with zeroes technique
} 

Number Pattern7

1
01
101
0101
10101
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=0;i<5;i++)
{
for(j=i+1;j<=(2*i+1);j++)
{
if(j%2==0)
{
printf("0");
}
else
{
printf("1");
}
}
printf("\n");
}
getch();
}

Number Pattern7

5              5
54          45
543      345
5432  2345
543212345
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
for(j=1;j<i;j++)
{
printf(" ");
}
for(j=2;j<i;j++)
{
printf(" ");
}
for(j=i;j<=5;j++)
{
if(j==1)
j++;
printf("%d",j);
}
printf("\n");
}
getch();
}

Number Pattern6

5
44
333
2222
11111
2222
333
44
5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf("");
}
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
for(i=2;i<=5;i++)
{
for(j=1;j<i;j++)
{
printf("");
}
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

Number Pattern5

1111
  222
    33
      4
    33
  222
1111
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=2;j<=i;j++)
{
printf(" ");
}
for(j=4;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}

for(i=3;i>=1;i--)
{
for(j=i;j>=2;j--)
{
printf(" ");
}
for(j=4;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

Number Pattern4

        5
      54
    543
  5432
54321
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(" ");
}
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

Number Pattern3

        1
      21
    321
  4321
54321
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=4;j>=i;j--)
{
printf(" ");
}
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

Number Pattern2

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91 
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,x;
clrscr();
x=1;
while(x<=12)
{
for(i=1;i<=79;)
{
for(j=i;j<i+x;j++)
{
printf("%d ",j);
}
printf("\n");
i=i+x;
x++;
}
}
getch();
}

star pattern2

        *
      **
    ***
  ****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
    {
        for(j=4;j>=i;j--)
        {
        printf(" ");
        }

    for(j=1;j<=i;j++)
    {
    printf("*"
    );
    }
printf("\n");
}
getch();
}

Number Pattern1

54321
5432
543
54
5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

Star pattern1

*****
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("*");
}
printf("\n");
}
getch();
}

Saturday, August 20, 2011

a tricky printf again...........

Here is a question which i am going to explore best to my concepts and knowledge,you can judge for yourself whether i am right or wrong..
int main()
{
int val=10;
printf(":%d",val+1,"-%d",val--);
return(0); 
}

Before evaluating this ques let us first evaluate the following printf statement
print(":%d",val+1,"-%d",val+2);


output for this printf:
:11
Explanation::As we know that printf first take the format string and according to the format specifiers in the format string it prints the arguments supplied to that..In this statement:
Format string is :%d
and the variable list id:val+a,"=%d",val+2
So this prints the val+1 that is 11 for the %d and leaves the another variables....

Now what if we want to print the remaining variables.......that so simple as we can see that the 2nd variable is a string and the third argument is an integer in the variable list so we should have the format string as given below:
format string----->:%d%s%d
and variables list is----->val+1, "-%d",val+2


so the output for this will be------>:11-%d12 
that is -%d is the string in this variable list so we have to use the %s format specifier to print this....


Now coming back to the real question,in the real question the printf statement is :
printf(":%d",val+1,"-%d",val--); 
If strictly speaking the result of this is undefined as you can read the sequence point post ,
No doubt the evaluation will be from right to left but we can't assure the result...but let us suppose everything goes according to our will or thinking that is smooth evaluation form right or left then first val-- will be evaluated that will decrement the val to 9 then val+1 which lead to print 10 as the output
so the output will be----->:10
To prove the evaluation is  from right to left let us print the value of val--;
printf(":%d%s%d",val+1,"-%d",val--);
output--->:10-%d10

You might not be satisfied with the prove of right to left evaluation so just change the val-- to --val because above example shows the postfix property that is printing before decrementing

printf(":%d%s%d",val+1,"-%d",--val);
output--->:10-%d9
Now i thing it will be cleared to you....

 May be i am wrong,so please do comment whether you agree with me or not????
if not give the explanation..... 

Tuesday, August 16, 2011

Saving Password in a file

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
char pw[100];
char *ptr;
int i=0;
FILE *fp;
clrscr();
printf("Enter the password:\n");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
pw[i]='\0';
--i;
}
else
{
printf("%c",ch);
putch('\b');
printf("*");
pw[i++]=ch;
}
ch=getch();
}
pw[i]='\0';
ptr=pw;
fp=fopen("c:\milan.txt","w");
while(*ptr!='\0')
{
fputc(*ptr,fp);
ptr++;
}
printf("\nPassword successfully saved in the file");
getch();
}

Comparing the password

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
char ch;
char pw[100];
char pswd[]="password";
int i=0;
clrscr();
printf("Enter the password:\n");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
pw[i]='\0';
--i;
}
else
{
printf("%c",ch);
putch('\b');
printf("*");
pw[i++]=ch;
}
ch=getch();
}
pw[i]='\0';
printf("\npassword is:%s",pw);
if(strcmp(pswd,pw)==0)
{
printf("\n password matched");
}
else
{
printf("\n password in wrong");
}
getch();
}

Saving the password in an array

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
char ch;
char pw[100];
int i=0;
clrscr();
printf("Enter the password:\n");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
pw[i]='\0';
--i;
}
else
{
printf("%c",ch);
putch('\b');
printf("*");
pw[i++]=ch;
}
ch=getch();
}
pw[i]='\0';
printf("\npassword is:%s",pw);
getch();
}

Delay in Password view

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
char ch;
clrscr();
printf("Enter the Password:\n");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
}
else
{
printf("%c",ch);
delay(500);
putch('\b');
printf("*");
}
ch=getch();
}
}

A program to implement the password view

This program will print the * whenever you press any digit and will also omit the last character after pressing the backspace..
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
ptintf("enter the password:\n");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
putch('\b');
putch(NULL);
putch('\b');
}
else
{
printf("%c",ch);
putch('\b');
printf("*");
}
ch=getch();
}
}