C program to check whether given string is palindrome or not

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,flag=1,len; char str[50]; clrscr(); printf(“Enter any string:”); gets(str); len=strlen(str); for(i=0,j=len-1;i<len/2;++i,–j) if(str[i]!=str[j]) { flag=0; break; } if(flag) printf(“String is palindrome“); else printf(“String is not palindrome”); getch(); }

C program to reverse a string

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,n; char a[30]; clrscr(); printf(“Enter any string:”); gets(a); n=strlen(a); printf(“Reverse of string:”); for(i=(n-1);i>=0;–i) printf(“%c”,a[i]); getch(); }

C program to read a string and print it in alphabetical order

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,n,ch1,ch2; char a[50],temp; clrscr(); printf(“Enter any string:”); scanf(“%s”,a); n=strlen(a); for(i=1;i<n;++i) for(j=0;j<(n-i);++j) { ch1=a[j]; ch2=a[j+1]; if(ch1>ch2) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf(“String after arranging %s”,a); getch(); }

C program to write student record to a binary file

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int rollno; char *name; int m1,m2,m3; }s1; void main() { FILE *fp; clrscr(); fp=fopen(“Record.dat”,”wb”); //opening binary file in writing mode if(fp==NULL) { printf(“File could not open”); exit(0); } printf(“Enter student detailsn”); printf(“Roll No:”); scanf(“%d”,&s1.rollno); printf(“Name:”); scanf(“%s”,s1.name); printf(“Marks in three subjects:”); scanf(“%d%d%d”,&s1.m1,&s1.m2,&s1.m3); fwrite(&s1,sizeof(s1),1,fp);     //writing to binary file printf(“nRecord has …

C program to write student record to a binary file Read More »

C program to copy the contents of one file into another

#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp1,*fp2; char ch,*f1,*f2; clrscr(); printf(“Enter source file name(ex:source.txt): “); scanf(“%s”,f1); printf(“Enter destination file name(ex:destination.txt): “); scanf(“%s”,f2); fp1=fopen(f1,”r”); fp2=fopen(f2,”w”); if(fp1==NULL||fp2==NULL) { printf(“File could not open!!”); exit(0); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }

C program to read data from keyboard and write it to a text file

#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; char ch,file[10]; clrscr(); printf(“Enter file name:”); scanf(“%s”,file); fp=fopen(file,”w”);   //file opening if(fp==NULL)   //exit program if file doesn’t open { printf(“File could not open!!”); exit(0); } printf(“Enter data(* to exit)n”); while(1) { ch=getche(); if(ch==’*’)      //exit when * is pressed exit(0); putc(ch,fp); } fclose(fp);   //file closing }