0% found this document useful (0 votes)
40 views

All C Programs Database

The document contains C code snippets demonstrating various string handling functions in C including: 1) Finding the length of a string using strlen(), copying a string using strcpy(), comparing strings using strcmp(), concatenating strings using strcat(), and reversing a string. 2) User-defined functions to find string length, copy a string, compare strings, concatenate strings, and reverse a string. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original string.

Uploaded by

asha.py81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

All C Programs Database

The document contains C code snippets demonstrating various string handling functions in C including: 1) Finding the length of a string using strlen(), copying a string using strcpy(), comparing strings using strcmp(), concatenating strings using strcat(), and reversing a string. 2) User-defined functions to find string length, copy a string, compare strings, concatenate strings, and reverse a string. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original string.

Uploaded by

asha.py81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Page 1/18

STRING {
// Finding string length using strlen char str[50];
#include<stdio.h> printf("Enter string");
#include<string.h> gets(str);
int main() strrev(str);
{ puts(str);
char a[50]; return 0;
int l; }
printf("Enter a string:");
gets(a); // Finding string length
l=strlen(a); int mystrlen(char []);
printf("Length of string is: %d",l); #include<stdio.h>
return 0; #include<string.h>
} int main()
{
// Copying a string using strcpy char str[50];
#include<stdio.h> int length;
#include<string.h> printf("Enter a string:");
int main() gets(str);
{ length=mystrlen(str);
char str1[50],str2[50]; printf("%d", length);
printf("Enter string"); return 0;
gets(str1); }
strcpy(str2,str1);
puts(str2); int mystrlen(char str[50])
return 0; {
} int i=0;
while(str[i]!='\0')
// Comparing strings using strcmp { i++;
#include<stdio.h> }
#include<string.h> return i;
int main() }
{ //String copying
char str1[50],str2[50]; #include<stdio.h>
int c; #include<string.h>
printf("Enter first string"); void mystrcpy(char str1[50],char str2[50]);
gets(str1); int main()
printf("Enter second string"); {
gets(str2); char str1[50],str2[50];
c=strcmp(str1,str2); printf("Enter string");
if(c==0) gets(str1);
printf("Strings are equal"); mystrcpy(str2,str1);
else puts(str2);
printf("strings are not equal"); return 0;
return 0; }
}
void mystrcpy(char str2[50],char str1[50])
// Concatenation strings using strcat {
#include<stdio.h> int s=0;
#include<string.h> while(str1[s]!='\0')
int main() {
{ str2[s]=str1[s];
char str1[50],str2[50]; s++;
int c; }
printf("Enter first string"); str2[s]='\0';
gets(str1); }
printf("Enter second string");
gets(str2); //String comparison
strcat(str1,str2); #include<stdio.h>
puts(str1); #include<string.h>
return 0; int mystrcmp(char [],char []);
} int main()
{
//string reverse char str1[50],str2[50];
#include<stdio.h> int c;
#include<string.h> printf("Enter first string");
int main() gets(str1);
Page 2/18

printf("Enter second string"); puts(str);


gets(str2); return 0;
c=mystrcmp(str1,str2); }
if(c==0) void mystrrev(char str[50])
printf("Strings are equal"); {
else char temp;
printf("Strings are not equal"); int i,l,j;
return 0; l=strlen(str);
} for(i=0,j=l-1;i<=j;i++,j--)
{
int mystrcmp(char str1[50],char str2[50]) temp=str[i];
//Function definition str[i]=str[j];
{ str[j]=temp;
int i=0; }
while (str1[i]!='\0' || str2[i]!='\0') }
{
if(str1[i]>str2[i]) // Use of fflush(stdin)
return 1; #include<stdio.h>
else if(str1[i]<str2[i]) int main()
return -1; {
char str[50];
int i;
i++; printf("Enter vowels");
} for(i=0;i<5;i++)
return 0; {
} fflush(stdin);
scanf("%c",&str[i]);
// string concatenation }
#include<stdio.h> for(i=0;i<5;i++)
#include<string.h> {
void mystrcat(char str1[50],char str2[50]); printf("%c",str[i]);
int main() }
{ }
char str1[50],str2[50];
printf("enter first string"); /*Write a c program to check whether the entered string is
gets(str1); palindrome or not.*/
printf("enter second string"); #include<stdio.h>
fflush(stdin); #include<string.h>
gets(str2); void mystrrev(char [],char []);
mystrcat(str1,str2); int mystrcmp(char [],char []);
puts(str1); int main()
return 0; {
} char str1[50],str2[50];
int c=0;
void mystrcat(char str1[50],char str2[50]) printf("Enter a string:");
{ gets(str1);
int i,l1,l2; mystrrev(str2,str1);
l1=strlen(str1);
l2=strlen(str2); c=mystrcmp(str1,str2);
if(c==0)
for(i=0;i<l2;i++) printf("Entered string is a palindrome");
{ else
str1[l1+i]=str2[i]; printf("Entered string is not a palindrome");
} return 0;
str1[l1+l2]='\0'; }
}
void mystrrev(char str2[50],char str1[50])
// String reverse {
#include<stdio.h> int i,l;
#include<string.h> l=strlen(str1)-1;
void mystrrev(char str[50]); for(i=0;i<=l;i++)
int main() {
{ char str[50]; str2[i]=str1[l-i];
printf("Enter string"); }
gets(str); str2[i]='\0';
mystrrev(str); }
printf("Reverse of entered string is ");
Page 3/18

int mystrcmp(char str1[50],char str2[50]) /*Write a c program to check characters of a string whether
{ character is capital case letter, small case letter, a digit
int i=0; or a special symbol.*/
while (str1[i]!='\0' || str2[i]!='\0') #include<stdio.h>
{ #include<string.h>
if(str1[i]>str2[i]) int main()
return 1; {
else if(str1[i]<str2[i]) char str[50];
return -1; int i=0;
printf("Enter a string:");
i++; gets(str);
} while(str[i]!='\0')
return 0; {
if(str[i]>='A' && str[i]<='Z')
} printf("%c is a capital letter",str[i]);
else if(str[i]>='a' && str[i]<='z')
/*Write a c program to toggle/invert the case of given string.*/ printf("\n%c is a small letter",str[i]);
#include<stdio.h> else if(str[i]>='0' && str[i]<='9')
#include<string.h> printf("\n%c is a Digit",str[i]);
int main() else
{ printf("\n%c is a Special symbol",str[i]);
char str[80]; i++;
int i; }
printf("Enter a string:"); return 0;
gets(str); }
i=0;
while(str[i]!='\0')
{
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32;

else if(str[i]>='a' && str[i]<='z')


str[i]=str[i]-32;

i++;
}
printf("Toggled string is ");
puts(str);
return 0;
}

/*Write a c program to count no. of vowels and consonants in the


entered string.*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
int i=0,vow=0,con=0;
printf("Enter a string:");
gets(str);
while(str[i]!='\0')
{
if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || str[i]=='i' ||
str[i]=='I' ||str[i]=='o' || str[i]=='O' ||str[i]=='u' || str[i]=='U')
vow++;
else
con++;

i++;
}
printf("No. of vowels: %d",vow);
printf("\nNo. of consonants: %d",con);
return 0;
}
Page 4/18

POINTERS p=&arr[n-1];
/*Write a c program to swap two no.s using pointers and function printf("Reverse : ");
*/ for(i=0;i<n;i++)
#include<stdio.h> {
void swap(int *,int *); printf("%d\t",*(p--));
int main() }
{ return 0;
int num1,num2; }
printf("Enter 2 no.s:");
scanf("%d%d",&num1,&num2); /*Write a C program using pointer to read an array of integers
printf("No.s before swapping are:%d,%d",num1,num2); and prints it's elements in reverse order */
swap(&num1,&num2); #include<stdio.h>
printf("No.s after swapping are:%d,%d",num1,num2); void reverse(int *,int);
return 0; int main()
} {
void swap(int *num1,int *num2) int arr[50],n,i;
{ printf("Enter no. of elements:");
int temp; scanf("%d",&n);
temp=*num1; printf("Enter %d elements in array",n);
*num1=*num2; for(i=0;i<n;i++)
*num2=temp; {
} scanf("%d",&arr[i]);
}
/* Write a c program to calculate area of a triangle by call by reverse(arr,n);
reference */ return 0;
#include<stdio.h> }
#include<math.h> void reverse(int *p,int n)
void area(int,int,int,float *); {
int main() int i;
{ printf("Reverse : ");
int a,b,c; for(i=n-1;i>=0;i--)
float ar=0.0; {
printf("Enter values of a,b and c:"); printf("%d ",*(p+i));
scanf("%d%d%d",&a,&b,&c); }
area(a,b,c,&ar); }
printf("The area of triangle is:%.2f",ar);
return 0; /*Write a C program to print average of elements of an array
} using pointer */
void area(int a,int b,int c,float *ar) #include<stdio.h>
{ float average(int *,int);
int s; int main()
s=(a+b+c)/2; {
if(a+b>c||b+c>a||a+c>b) int arr[50],i,n;
{ int *p=arr;
*ar=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Enter size of array : ");
} scanf("%d",&n);
else printf("Enter elements of array : \n");
{ for(i=0;i<n;i++)
printf("Invalid triangle"); {
} scanf("%d",&arr[i]);
} }
float avg=average(p,n);
/*Write a C program using pointer to read an array of integers printf("Average of array : %.2f",avg);
and prints it's elements in reverse order */ return 0;
#include<stdio.h> }
int main() float average(int *p,int n)
{ {
int arr[50],i,n; int i,sum=0;
int *p=arr; float avg;
printf("Enter Range : "); for(i=0;i<n;i++)
scanf("%d",&n); {
for(i=0;i<n;i++) sum=sum+*(p++);
{ }
scanf("%d",&arr[i]); avg=(float)sum/n;
} return avg;
}
Page 5/18

/*Write a C program to reverse elements of an array using pointer POINTER AND STRING
and function.*/ /*Finding string length using pointer */
#include<stdio.h> int mystrlen(char *);
void reverse(int *,int); #include<stdio.h>
int main() #include<string.h>
{ int main()
int arr[50],*ap,n,i; {
ap=arr; char str[50],*s;
printf("Enter no. of elements:"); int length;
scanf("%d",&n); printf("Enter a string:");
printf("\nEnter elements:"); gets(str);
for(i=0;i<n;i++) s=str;
{ length=mystrlen(s);
scanf("%d",ap+i); printf("%d", length);
} return 0;
}
reverse(ap,n);
printf("\nThus formed array is:"); int mystrlen(char *s)
for(i=0;i<n;i++) {
{ int i=0;
printf("\n%d",*(ap+i)); while(*s!='\0')
} {
} i++;
s++;
void reverse(int *ap,int n) }
{ return i;
int temp,i,j; }
for(i=0,j=n-1;i<j;i++,j--)
{
temp=*(ap+i);
*(ap+i)=*(ap+j); /*Write a C program to peform String comparison using pointer*/
*(ap+j)=temp; #include<stdio.h>
} #include<string.h>
} int mystrcmp(char *,char *);
int main()
/*Write a C program to check whether number is Armstrong or {
not, using pointer and function.*/ char str1[50],str2[50];
#include<stdio.h> int c;
#include<math.h> printf("Enter first string");
int armstrong(int *); gets(str1);
int main() printf("Enter second string");
{ gets(str2);
int n,*p,temp,sum; c=mystrcmp(str1,str2);
p=&n; if(c==0)
printf("Enter a number:"); printf("Strings are equal");
scanf("%d",&n); else
temp=n; printf("Strings are not equal");
sum=armstrong(p); return 0;
if(sum==temp) }
printf("Entered no. is an Armstrong number");
else int mystrcmp(char *p1,char *p2)
printf("Entered no. is not an armstrong number"); {
} int i=0;
while (*p1!='\0' || *p2!='\0')
int armstrong(int *p) {
{ if(*p1>*p2)
int rem,sum=0; return 1;
while((*p)!=0) else if(*p1<*p2)
{ return -1;
rem=(*p)%10;
sum=sum+pow(rem,3); p1++;
*p=(*p)/10; p2++;
} }
return sum; return 0;
} }
Page 6/18

STRUCTURES /*Write a program to store and print information of a student


/*write a programme to illustrate the different ways of where information comprises of student’s name, sno and marks
declaration and initialization of structure variable by storing in 4 subjects*/
information of three student*/ #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<stdio.h> int main()
#include<string.h> {
int main() typedef struct
{ {
//Different ways of declaring structure variable int sno;
struct info char name[20];
{ int submarks[4];
int sno; }student;
char name[20]; student s1;
char add[20]; int j;
}s1,s2; printf("enter the details of students\n");
printf("enter sno and name\n");
//Different ways of initialising structure varibles scanf("%d%s",&s1.sno,s1.name);
struct info stu1={1,"ajay","dehradun"}; printf("enter the marks in 4 subjects\n");
for(j=0;j<4;j++)
struct info stu2,stu3; {
scanf("%d",&s1.submarks[j]);
stu2.sno=2; }
//stu2.name="rashmi";//wrong printf("the details of students are\n");
strcpy(stu2.name,"rashmi"); printf("sno-%d name-%s\n",s1.sno,s1.name);
strcpy(stu2.add,"delhi"); printf("the marks in 4 subjects\n");
for(j=0;j<4;j++)
printf("enter the sno,name and add of third student\n"); {
scanf("%d%s%s",&stu3.sno,stu3.name,stu3.add); printf("marks in %d subject is %d\n",j,s1.submarks[j]);
}
printf("\ndetails of students are\n"); return 0;
printf("\ndetails of 1st stu-> }
%d\t%s\t%s",stu1.sno,stu1.name,stu1.add);
printf("\ndetails of 2nd stu-> /*Write a program to store and print information of n students
%d\t%s\t%s",stu2.sno,stu2.name,stu2.add); where information comprises of student's name, sno and marks in
printf("\ndetails of 3rd stu-> 4 subjects*/
%d\t%s\t%s",stu3.sno,stu3.name,stu3.add); #include<stdio.h>
return 0; #include<conio.h>
} int main()
{
/*Write a program to store and print information of a student typedef struct
where information Comprises of student's name, sno and marks {
in 4 subjects*/ int sno;
#include<stdio.h> char name[20];
#include<conio.h> int submarks[4];
int main() }student;
{
typedef struct student s1[10];
{ int i,n,j;
int sno; printf("enter the no of students\n");
char name[20]; scanf("%d",&n);
printf("enter the details of students\n");
}student; for(i=0;i<n;i++)
{
student s1; printf("enter sno and name\n");
scanf("%d%s",&s1[i].sno,s1[i].name);
printf("Enter the details of students\n"); printf("enter the marks in 4 subjects\n");
printf("Enter sno and name\n"); for(j=0;j<4;j++)
scanf("%d%s",&s1.sno,s1.name); {
scanf("%d",&s1[i].submarks[j]);
printf("The details of students are\n"); }
printf("Sno-%d Name-%s\n",s1.sno,s1.name); }
printf("the details of students are\n");
return 0; for(i=0;i<n;i++)
} {
printf("sno-%d name-%s\n",s1[i].sno,s1[i].name);
printf("the marks in 4 subjects\n");
Page 7/18

for(j=0;j<4;j++) float per;


{ };
printf("marks in %d subject is %d\n",j,s1[i].submarks[j]); int total=0,n,i,j;
} float high;
} int index;
return 0; struct info s[50];
} printf("enter the no. of student\n");
scanf("%d",&n);
/*20. Write a c program to input Name, roll no. and percentage of for(i=0;i<n;i++)
n students. Calculate average percentages of class and print the {
details of all students having percentage greater than or equal to printf("enter roll no.:");
average percentage.*/ scanf("%d",&s[i].rn);
#include<stdio.h> printf("enter name\n");
#include<string.h> fflush(stdin);
struct stu gets(s[i].name);
{ printf("enter marks in 4 sub\n");
char Name[15]; for(j=0;j<4;j++)
int Rn; {
float per; scanf("%d",&s[i].marks[j]);
}s1[100]; }
int main() }
{
int i,n; for(i=0;i<n;i++)
float avgper,sumper; {
printf("Enter no. of Students:"); total=0;
scanf("%d",&n); for(j=0;j<4;j++)
for(i=0;i<n;i++) {
{ total=total+s[i].marks[j];
printf("Name:"); }
fflush(stdin); //initializing "per" member of each student
gets(s1[i].Name); s[i].per=(float)total/4;
printf("Roll no. :"); }
scanf("%d",&s1[i].Rn); //Assuming first field as highest per.
printf("Enter percentage"); high=s[0].per;
scanf("%f",&s1[i].per); index=0;
}
//calculating sum of percentage of class //Finding index of Highest percentage in class
for(i=0;i<n;i++) for(i=1;i<n;i++)
sumper=sumper+s1[i].per; {
if(s[i].per>high)
//Average percentage of whole class {
avgper=(float)sumper/n; high=s[i].per;
index=i;
printf("Student(s) with percentage greater than }
average\n");
for(i=0;i<n;i++) }
{ //printing details of students with highest percentage
if(avgper<=s1[i].per) printf("highest percentage is %f\n",high);
{ printf("name of student is:");
printf("\nName:"); puts(s[index].name);
puts(s1[i].Name); printf("\nroll no. of student is %d:",s[index].rn);
printf("\nRoll no. %d",s1[i].Rn); return 0;
} }
}
return 0; /*Define a structure data type called time_struct containing three
} members integer hours, integer minutes, integer seconds.
Write a program that would assign values to the individual
/*Write a C program to enter name, roll no., marks in 4 sub of n members and display the time in the following form: 12:42:58.
students then calculate percentage for each student and print Create functions insert() and display() for input and Output*/
name and roll no. of student with highest percentage*/ #include<stdio.h>
#include<stdio.h> #include<string.h>
int main() struct time_struct
{ { int hours;
struct info{ int minutes;
int rn ; int seconds;
char name[20]; };
int marks[4]; struct time_struct insert(struct time_struct);
Page 8/18

void display(struct time_struct); printf("dob-


int main() %d%d%d\n",e[i].dob.day,e[i].dob.mon,e[i].dob.year);
{ printf("doj-%d%d%d\n",e[i].doj.day,e[i].doj.mon,e[i].doj.year);
struct time_struct t1,t2; }
t2=insert(t1); return 0;
display(t2); }
return 0;
} /*Write a C program to accept records of employees.
struct time_struct insert(struct time_struct t) The structure is:
{ Struct emp
printf("Enter hours:" ); { char name[20];
scanf("%d",&t.hours); int age;
printf("Enter minutes:" ); int basic;
scanf("%d",&t.minutes); }
printf("Enter seconds:" ); calculate total salary as-total salary=basic+hra+da
scanf("%d",&t.seconds); da=10% of basic hra=5% of basic salary
return t; Display age, total salary of employee in descending order on the
} basis of total salary. Create separate function for input &
display.*/
void display(struct time_struct t) struct emp
{ { char name[20];
printf("Time: %d:%d:%d",t.hours,t.minutes,t.seconds); int age;
} int basic;
};
/*Write a C program to store the details of an employee where
details consist of employye's sno, name, dob and doj #include<stdio.h>
(Structure within Structure)*/ void input(struct emp []);
void display(struct emp []);
#include<stdio.h> int main()
#include<conio.h> {
typedef struct date struct emp s[5],temp;
{ int i,j;
int day; input(s);
int mon;
int year; for(i=0;i<5;i++)
}date; {
for(j=0;j<5-1;j++)
typedef struct {
{ if(s[j].basic<s[j+1].basic)
int sno; {
char name[20]; temp=s[j];
date dob,doj; s[j]=s[j+1];
}employee; s[j+1]=temp;
}
int main() }
{ }
int i,n;
employee e[10]; display(s);
return 0;
printf("enter the no employees\n"); }
scanf("%d",&n); void input(struct emp s[5])
printf("enter the details of employee\n"); {
for(i=0;i<n;i++) int i;
{ for(i=0;i<5;i++)
printf("enter the sno and name of employee\n"); {
scanf("%d%s%",&e[i].sno,e[i].name); printf("Enter Name : ");
printf("enter the dob of employee\n"); fflush(stdin);
scanf("%d%d%d",&e[i].dob.day,&e[i].dob.mon,&e[i].dob.year); gets(s[i].name);
printf("enter the doj of employee\n"); printf("Enter age : ");
scanf("%d%d%d",&e[i].doj.day,&e[i].doj.mon,&e[i].doj.year); scanf("%d",&s[i].age);
} printf("Enter Basic Salary : ");
printf("the details of employee are \n"); scanf("%d",&s[i].basic);
for(i=0;i<n;i++) }
{ }
printf("\n"); void display(struct emp s[5])
printf("sno-%d\nname-%s\n",e[i].sno,e[i].name); {
int i;
Page 9/18

float total,hra,da;
for(i=0;i<5;i++) printf("enter the grade\n");
{ fflush(stdin);
printf("\nName : "); scanf("%c",&u1.grade);
puts(s[i].name); printf("grade->%c",u1.grade);
printf("Age : %d\n",s[i].age);
da=0.1*s[i].basic; //Now printing complete details
hra=0.05*s[i].basic; printf("\nname-%s\n",u1.name);//Garbage
total=s[i].basic+hra+da; printf("\nroll no-%d\n",u1.roll);//Garbage
printf("Total Salary : %f",total); printf("\npercentage-%f\n",u1.per);//Garbage
} printf("\ngrade-%c",u1.grade);
}
return 0;
/*Write a C program to read and print the values of a structure }
variable using structure pointer*/
#include<stdio.h> /*Write a C program to compare the memory allocations between
#include<conio.h> structure and union variable*/
int main() #include<stdio.h>
{ #include<conio.h>
typedef struct student int main()
{ {
int rollno; typedef union
char name[20]; {
}stud; int roll;
stud s1,s2,*p1, *p2; char grade;
p1=&s1; float per;
p2=&s2; }uni1;

printf("First Method \nenter the roll no and name\n"); typedef struct


scanf("%d%s", &(*p2).rollno,(*p2).name); {
printf("rollno-%d name-%s",(*p2).rollno,(*p2).name); int roll;
printf("\n\n"); char grade;
printf("Second Method \nenter the roll no and name\n"); float per;
scanf("%d%s",&(p1->rollno),p1->name); }str1;
printf("rollno-%d name-%s",p1->rollno,p1->name); uni1 u1;
str1 s1;
return 0; printf("\nSTRUCTURE");
} printf("\nsize of s1 is %d",sizeof(s1));
printf("\naddress of s1 is %u",&s1);
UNION printf("\naddress of s1.roll is %u",&s1.roll);
/*Write a program to read and print the following information printf("\naddress of s1.grade is %u",&s1.grade);
using union :name, rollno, percentage, grade*/ printf("\naddress of s1.per is %u",&s1.per);
#include<conio.h>
#include<stdio.h> printf("\nUNION");
int main() printf("\nsize of u1 is %d",sizeof(u1));
{ printf("\naddress of u1 is %u",&u1);
union student printf("\naddress of u1.roll is %u",&u1.roll);
{ printf("\naddress of u1.grade is %u",&u1.grade);
char grade; printf("\naddress of u1.per is %u",&u1.per);
char name[20];
int roll; return 0;
float per; }

}u1;

printf("enter the name\n");


gets(u1.name);
printf("name->%s\n",u1.name);

printf("enter the roll\n");


scanf("%d",&u1.roll);
printf("roll no->%d\n",u1.roll);

printf("enter the percentage\n");


scanf("%f",&u1.per);
printf("percentage->%f\n",u1.per);
Page 10/18

FILES while( (ch=getc(fp1))!=EOF )


/*Write a C program to store character in a file and print the {
characters after reading from file*/ putc(ch,fp2);
#include<stdio.h> }
int main() printf("\nCopied !\n");
{
FILE *fp1; fclose(fp1);
char ch; fclose(fp2);
fp1=fopen("Master.txt","w"); return 0;
if(fp1==NULL) }
{ /*Write a C program to write alphabets to a file then read all
printf("File creation error"); alphabets from file and store in another file by converting all
return 0; alphabets to UPPERCASE*/
} #include<stdio.h>
int main()
printf("Enter characters:\n"); {
while((ch=getchar())!=EOF) FILE *fp1,*fp2;
{ char ch;
fputc(ch,fp1); fp1=fopen("source.txt","w");
} if(fp1==NULL)
fclose(fp1); {
fp1=fopen("Master.txt","r"); printf("File creation error");
if(fp1==NULL) return 0;
{ }
printf("file can not be copied");
return 0; printf("Enter characters:\n");
} while((ch=getchar())!=EOF)
{
printf("The characters of file are:\n"); putc(ch,fp1);
while( (ch=fgetc(fp1))!=EOF ) }
{ fclose(fp1);
putchar(ch); fp1=fopen("source.txt","r");
} fp2=fopen("copy_upper.txt","w");
fclose(fp1); if(fp1==NULL||fp2==NULL)
{
return 0; printf("file can not be copied");
} return 0;
}
/*Write a C program to copy character of a file into another file*/ while( (ch=getc(fp1))!=EOF )
#include<stdio.h> {
int main() ch=toupper(ch);//Converting alphabet to Upper
{ case
FILE *fp1,*fp2; putc(ch,fp2);//writing converted character to
char ch; file fp2
fp1=fopen("Master.txt","w");
if(fp1==NULL) }
{ fclose(fp1);
printf("File creation error"); fclose(fp2);
return 0; return 0;
} }
/*Write a C program to copy vowels consonants and special
printf("Enter characters:\n"); characters to four separate file vowel.txt, consonant.txt,
while((ch=getchar())!=EOF) special.txt, digit.txt*/
{ #include<stdio.h>
putc(ch,fp1); int main()
} {
fclose(fp1); FILE *fp1,*fp2,*fp3,*fp4,*fp5;
fp1=fopen("Master.txt","r"); char ch;
fp2=fopen("copy.txt","w"); fp1=fopen("Master.txt","w");
if(fp1==NULL||fp2==NULL) if(fp1==NULL)
{ {
printf("file can not be copied"); printf("File creation error");
return 0; return 0;
} }
printf("Enter characters:\n");
printf("\nCopying....:\n"); //reading from user till ctrl+z(EOF) are pressed
while((ch=getchar())!=EOF)
Page 11/18

{ {
putc(ch,fp1);// Writing each read character to printf("%d\n",number);
file fp1 }
} fclose(fp1);
fclose(fp1);
//now opeing 5 files, 1 source file & 4 target files return 0;
fp1=fopen("Master.txt","r"); }
fp2=fopen("vowel.txt","w");
fp3=fopen("const.txt","w"); /*Write a C program to store even and odd numbers in two
fp4=fopen("digit.txt","w"); different files*/
fp5=fopen("Special.txt","w"); #include<stdio.h>
if(fp1==NULL||fp2==NULL||fp3==NULL||fp4==NULL int main()
||fp5==NULL) {
FILE *fp1,*fp2,*fp3;
{ printf("error");
int number;
} fp1=fopen("Master.dat","w");
while((ch=getc(fp1))!=EOF) if(fp1==NULL)
{ {
printf("File creation error");
if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch return 0;
=='I') }
||(ch=='o')||(ch=='O')||(ch=='u')||(ch=='U'))
{ putc(ch,fp2); printf("Enter integers:\n");
} while(number!=EOF)
{
else scanf("%d",&number);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) putw(number,fp1);
{ putc(ch,fp3); }
}
else if(ch>='0'&&ch<='9') fclose(fp1);
{ putc(ch,fp4);
} fp1=fopen("Master.dat","r");
else fp2=fopen("even.dat","w");
{ putc(ch,fp5); fp3=fopen("odd.dat","w");
if(fp1==NULL||fp2==NULL||fp3==NULL )
} {
} printf("file can not be copied");
fclose(fp1); return 0;
fclose(fp2);fclose(fp3);fclose(fp4);fclose(fp5); }
return 0; while( (number=getw(fp1))!=EOF )
} {
if(number%2==0)
/*Write a C program to store integers in a file and print the putw(number,fp2);
integers after reading from file*/ else
putw(number,fp3);
#include<stdio.h> }
int main() fclose(fp1);fclose(fp2);fclose(fp3);
{
FILE *fp1; fp2=fopen("even.dat","r");
int number; if(fp2==NULL)
fp1=fopen("Master.dat","w"); {
if(fp1==NULL) printf("file reading error in even file");
{ return 0;
}
printf("File creation error");
return 0; printf("\nContents of even file\n");
} while( (number=getw(fp2))!=EOF)
{
printf("Enter integers:\n"); printf("%d\n",number);
while(number!=EOF) }
{ fclose(fp2);
scanf("%d",&number);
putw(number,fp1); fp3=fopen("odd.dat","r");
if(fp3==NULL)
}
{
fclose(fp1); printf("file reading error in odd file");
fp1=fopen("Master.dat","r"); return 0;
if(fp1==NULL) }
{ printf("\nContents of odd file\n");
printf("file can not be copied"); while( (number=getw(fp3))!=EOF )
return 0; {
} printf("%d\n",number);
while( (number=getw(fp1))!=EOF ) }
Page 12/18

fclose(fp3); {
fputs(str,fp1);
return 0; fputs("\n",fp1);
}
}
}
fclose(fp1);
/*Write a C program to store string inputted by user to files and
print string after reading from file*/ fp1=fopen("test2.txt","r");
if(fp1==NULL)
#include<stdio.h> {
int main() printf("file does not exist\n");
{ return 0;
char str[30]; }
FILE *fp1; else
{
fp1=fopen("test2.txt","w"); while(fgets(str,30,fp1)!=NULL)
if(fp1==NULL) {
{ if((strstr(str,"Hello"))!=NULL)
printf("file creation error\n"); {
return 0; printf("Found ");
} }
else }
{ }
printf("enter the text\n"); fclose(fp1);
while(gets(str)!=NULL) return 0;
{ }
fputs(str,fp1);
fputs("\n",fp1); /*Write a C program to count number of lines in a file*/
} #include<stdio.h>
} int main()
fclose(fp1); {
char str[30];
fp1=fopen("test2.txt","r"); FILE *fp1;
if(fp1==NULL) char ch;
{ int c=0;
printf("file does not exist\n");
return 0; fp1=fopen("test2.txt","w");
} if(fp1==NULL)
else {
{ printf("file creation error\n");
while(fgets(str,30,fp1)!=NULL) return 0;
{ }
puts(str); else
} {
} printf("enter the text\n");
fclose(fp1); while(gets(str)!=NULL)
return 0; {
} fputs(str,fp1);
fputs("\n",fp1);
/*Write a C program to search a string/word inputted by user in a }
file*/ }
#include<stdio.h> fclose(fp1);
int main()
{ fp1=fopen("test2.txt","r");
char str[30]; if(fp1==NULL)
FILE *fp1; {
printf("file does not exist\n");
fp1=fopen("test2.txt","w"); return 0;
if(fp1==NULL) }
{
printf("file creation error\n"); while((ch=fgetc(fp1))!=EOF)
return 0; {
} if(ch=='\n')
else c++;
{ }
printf("enter the text\n"); printf("%d", c);
while(gets(str)!=NULL) fclose(fp1);
Page 13/18

return 0;
}

ALL THE BEST!


-Rajendra Singh Bisht

You might also like