All C Programs Database
All C Programs Database
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
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;
i++;
}
printf("Toggled string is ");
puts(str);
return 0;
}
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
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;
}u1;
{ {
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;
}