101. Write a program accept a file name from user and print content of that file

#include<stdio.h>
int main(int argc, char *argv[])
{
    if ( argc != 2 ){
         printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        FILE *file = fopen( argv[1], "r" );
 
        if(file == NULL)
             printf("Could not open file\n");
        else 
        {
            int x;
            while((x=fgetc(file)) != EOF)
                 printf( "%c", x );
            fclose( file );
        }
    }
}

100. Merge two files by store data into third one

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
   FILE *fp1 = fopen("file1.txt", "r");
 
   FILE *fp2 = fopen("file2.txt", "r");
 
   FILE *fp3 = fopen("file3.txt", "w");
 
   char c;
   if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
   {
         puts("Could not open files");
         exit(0);
   }
 
   while ((c = fgetc(fp1)) != EOF)
      fputc(c, fp3);
 
   while ((c = fgetc(fp2)) != EOF)
      fputc(c, fp3);
 
   fclose(fp1);
   fclose(fp2);
   fclose(fp3);
   return 0;
}

099. Write a program to read and write a file line by line.

#include <stdio.h>
int main()
{
    FILE *fp;
    char str[100];
    fp = fopen("C:\\myfile.txt", "w");
 
    if (fp == NULL)
    {
       puts("An error occurred while opening the specified file");
    }
 
     printf("Enter your string:");
 
     gets(str);
 
     fputs(str, fp);
 
     fclose(fp);
 
    fp = fopen("C:\\myfile.txt", "r");
 
    if (fp == NULL)
    {
       puts("An error occurred while opening the specified file");
    }
 
    while(1)
    {
       if(fgets(str, 10, fp) ==NULL)
            break;
       else
            printf("%s", str);
    }
 
    fclose(fp);
    return 0;
}

098. Write a program to read and write a file character by character.

#include<stdio.h>
int main()
{
    FILE *fp;   /* file pointer*/
    char fName[20];
    char ch;
 
    printf("\nEnter file name to create :");
    scanf("%s",fName);
 
    fp = fopen(fName,"w");
    if(fp==NULL)
    {
        printf("File not created !!");
        exit(0); 
    }
 
    printf("File created successfully.");
    /*writing into file*/
    printf("\nEnter text to write (press < enter > to save & quit):\n");
    while( (ch = getchar())!='\n')
    {
        putc(ch, fp); /*write character into file*/
    }
 
/*again open file to read data*/
    fp = fopen(fName,"r");
    if(fp==NULL)
    {
        printf("\nCan't open the file!!!");
        exit(0);
    }
 
    printf("\nContents of file is :\n");
    /*read text until, end of file is not detected*/
    while( (ch=getc(fp))!=EOF )
    {
        printf("%c",ch); /*print character on screen*/
    }
 
    fclose(fp);
    return 0;
}

097. Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)

#include<stdio.h>
#include<conio.h>
 
struct Date
{
	int mm,dd,yy;
};
 
struct Employee
{
char name[50];
 int emp_id;
 struct Date DOB,DOJ;
};
 
main()
{   int i;
	struct Employee e;
	clrscr();
		printf("\nEnter name           : ");
		scanf("%s",&e.name);
		printf("\nEnter emp_id.        : ");
		scanf("%d",&e.emp_id);
		printf("\nEnter Date of Joining\n ");
		printf("(dd-mm-yy) : ");
		scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
		printf("\nEnter Date of birth\n ");
		printf("(dd-mm-yy) : ");
		scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
 
		printf("\nName           : %s",e.name);
		printf("\nEmployee ID    : %d",e.emp_id);
		printf("\nEnter DOJ      : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
		printf("\nEnter DOB      : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
 
	getch();
}

096. Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint

 
#include<stdio.h>
#include<conio.h>
 
struct Student
{
 char name[50];
 int roll_no;
 int m1,m2,m3;
};
 
main()
{   int i;
	struct Student s[5];
	clrscr();
	for(i=0;i<5;i++)
	{   printf("\nEnter data for Student %d.....\n",i+1);
		printf("Enter name           : ");
		scanf("%s",&s[i].name);
		printf("Enter Roll No.       : ");
		scanf("%d",&s[i].roll_no);
		printf("Enter marks for sub1 : ");
		scanf("%d",&s[i].m1);
		printf("Enter marks for sub2 : ");
		scanf("%d",&s[i].m2);
		printf("Enter marks for sub3 : ");
		scanf("%d",&s[i].m3);
	}
 
	for(i=0;i<5;i++)
	{   printf("\nStudent   %d\n",i+1);
		printf("Name    : %s\n",s[i].name);
		printf("Roll No.: %d\n",s[i].roll_no);
		printf("Sub1    : %d\n",s[i].m1);
		printf("Sub2    : %d\n",s[i].m2);
		printf("Sub3    : %d\n",s[i].m3);
	}
 
	getch();
}

095. Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it

#include<stdio.h>
#include<conio.h>
 
struct Employee
{
 char name[50];
 int emp_id;
 long phone_no;
};
 
main()
{
	struct Employee e;
	clrscr();
	printf("Enter name : ");
	scanf("%s",&e.name);
	printf("Enter emp_id: ");
	scanf("%d",&e.emp_id);
	printf("Enter Phone Number: ");
	scanf("%ld",&e.phone_no);
 
	printf("\n\nEnter name : %s",e.name);
	printf("\n\nEnter Emp Id       : %d",e.emp_id);
	printf("\n\nEnter Phone Number : %ld ",e.phone_no);
 
	getch();
}

094. Write a program to check if the given string is palindrome or not

E.g. NITIN, LIRIL, MALAYALAM.

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
	int i,j,len=0;
	char *s1,*s2;
	clrscr();
	printf("Enter the string ....");
	scanf("%s",s1);
	for(i=0;s1[i]!='\0';i++)
		len++;
	j=0;
	for(i=len-1;i>=0;i--)
	{
		s2[j]=s1[i];
		j++;
	}
	s2[j]='\0';
	for(i=0;s1[i]!='\0';i++)
		if(s1[i]!=s2[i])
		{
			printf("\nNot a palindrome");
			getch();
			exit(0);
	}
	printf("Palindrome");
	getch();
}

093. Write a program to reverse the given string in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,len=0;
	char *s1,*s2;
	clrscr();
	printf("Enter the string ....");
	scanf("%s",s1);
	for(i=0;s1[i]!='\0';i++)
		len++;
	j=0;
	for(i=len-1;i>=0;i--)
	{
		s2[j]=s1[i];
		j++;
	}
	s2[j]='\0';
	printf("\nReversed String : %s",s2);
	getch();
}

092. Write a program to count all vowels present in the string in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,vowel=0;
	char *s;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s);
 
	for(i=0;s[i]!='\0';i++)
	{
		if((s[i]=='A')||(s[i]=='E')||(s[i]=='O')||(s[i]=='U')||(s[i]=='I')||
		  (s[i]=='a')||(s[i]=='e')||(s[i]=='o')||(s[i]=='u')||(s[i]=='i'))
						vowel++;
	}
	printf("\nVowels : %d",vowel);
	getch();
}