Thursday, 16 June 2011

An Array of String (Security)

#define MAX 5
#define LEN 40


void main(void)
{
 int dex;
 int enter=0;
 char name[40];
 static char list[MAX][LEN]={ "Muzammil", "Nigel", "Katrina",
                              "Alistair" };
 printf("Enter your name:"):
 gets(name);
 for(dex=0;dex<MAX;dex++)
  if(strcmp(&list[dex][0],name)==0)
   enter = 1;
 if(enter==1)
   printf("You may enter, oh honored one.");
 else
   printf("Guards! Remove this person!");
 }


Output:
There are two possible outcomes when you interact with this program. Either your name is on the list:


Enter your name: Muzammil
You may enter, oh honored one.


or it isn't:


Enter your name: George
Guards! Remove this person!

The String I/O Functions gets() and puts()

void main(void)
{
 char name[81];


 puts("Enter your name: ");
 gets(name);
 puts("Greetings, ");
 puts(name);
}


Output:


Enter your name: 
Muzammil Versiani
Greetings, 
Muzammil Versiani

Thursday, 9 June 2011

Switch Case in C


    The basic form of a switch statement is:
    switch (variable) { case expression1: do something 1; break; case expression2: do something 2; break; .... default: do default processing; }
     In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.
    Another Example of Switch Case Program:
    switch( marks )      {         case 'A' : printf( "A++" );                    break;         case 'B' : printf( "A+" );            break; case 'C' : printf( "A" );            break;         case 'D' : printf( "B" );            break; case 'F' : printf( "C" );               break; default  : printf( "YOU FAILED" );                    break; }
    The keyword break must be included at the end of each case statement. The default clause is optional
    We also have a continue statement which is used with switch case.
    A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

Switch Case in C


    The basic form of a switch statement is:
    switch (variable) { case expression1: do something 1; break; case expression2: do something 2; break; .... default: do default processing; }
     In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.
    Another Example of Switch Case Program:
    switch( marks )      {         case 'A' : printf( "A++" );                    break;         case 'B' : printf( "A+" );            break; case 'C' : printf( "A" );            break;         case 'D' : printf( "B" );            break; case 'F' : printf( "C" );               break; default  : printf( "YOU FAILED" );                    break; }
    The keyword break must be included at the end of each case statement. The default clause is optional
    We also have a continue statement which is used with switch case.
    A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

Wednesday, 1 June 2011

String Variables

void main(void)
{
 char name[81];
 printf("Enter your Name: ");
 scanf("%s",name);
 printf("Welcome!, %s",name);
 getch();
}


Output:


Enter your Name: Muzammil
Welcome!, Muzammil




If you have any question, you can ask me freely!

Sorting an Array (increasing order)

#define MAXSIZE 20
void sort(int[],int);


void main (void)
{
 static int list[MAXSIZE];
 int size=0;
 int dex;
 do
  {
   printf("Type Number: ");
   scanf("%d",&list[size]);
  } 
 while(list[size++] != 0);
 sort(list,--size);
 for(dex=0;dex<size;dex++)
  printf("%d\n",list[dex]);


}
void sort(int[list],int size)
 {
  int out,in,temp;
  for(out=0;out<size-1;out++)
   for(in=out+1;in<size;in++)
    if(list[out]>list[in])
      {
       temp = list[in];
       list[in] = list[out];
       list[out] = temp;
      }
}


Output:
Type Number: 46
Type Number: 25
Type Number: 73
Type Number: 58
Type Number: 33
Type Number: 18
Type Number: 0
18
25
33
46
58
73


If you have any question, you ask me freely!