Google Analytics

Friday 19 August 2011

C Programs during trng


  1.  /*  Program 7 - ANYFILE.C */
#include <stdio.h>
#include <stdlib.h>


int main()
{
FILE *fp1;
char oneword[100], filename[25];
char *c;


   printf("Enter filename -> ");
   scanf("%s", filename);            /* read the desired filename  */
   fp1 = fopen(filename, "r");
   if (fp1 == NULL)
   {
      printf("File failed to open\n");
      exit (EXIT_FAILURE);
   }


   do 
   {
      c = fgets(oneword, 100, fp1);  /* get one line from the file */
      if (c != NULL)                
         printf("%s", oneword);      /* display it on the monitor  */
   } while (c != NULL);              /* repeat until NULL          */


   fclose(fp1);


   return EXIT_SUCCESS;
}




/* Result of execution


(The file selected is listed on the monitor)


*/







                             /*   Program 6 - BACKWARD.C */
#include <stdio.h>       /* Prototypes for standard Input/Output   */
#include <string.h>      /* Prototypes for string operations       */


void forward_and_backwards(char line_of_char[], int index);


int main()
{
char line_of_char[80];
int index = 0;


   strcpy(line_of_char, "This is a string.\n");


   forward_and_backwards(line_of_char, index);


   return 0;
}



void forward_and_backwards(char line_of_char[], int index)
{
   if (line_of_char[index]) 
   {
      printf("%c", line_of_char[index]);
      index++;
      forward_and_backwards(line_of_char, index);
   }
   printf("%c", line_of_char[index]);
}





/* Result of execution


This is a string.


.gnirts a si sih


*/

 /* Pointer*/



#include <stdio.h>
#include <string.h>


int main()
{
char strg[40], *there, one, two;
int  *pt, list[100], index;


   strcpy(strg, "This is a character string.");


   one = strg[0];                    /* one and two are identical */
   two = *strg;
   printf("The first output is %c %c\n", one, two);


   one = strg[8];                   /* one and two are indentical */
   two = *(strg+8);
   printf("The second output is %c %c\n", one, two);


   there = strg+10;          /* strg+10 is identical to &strg[10] */
   printf("The third output is %c\n", strg[10]);
   printf("The fourth output is %c\n", *there);


   for (index = 0 ; index < 100 ; index++)
      list[index] = index + 100;
   pt = list + 27;
   printf("The fifth output is %d\n", list[27]);
   printf("The sixth output is %d\n", *pt);


   return 0;
}




/* Result of execution


The first output is T T
The second output is a a
The third output is c
The fourth output is c
The fifth output is 127
The sixth output is 127


*/

1 comment: