Introducing you some important programs of C language with coding . These programs are provided by Mr. Anuj Khanna (Asst. Professor),Krishna Institute of Technology,Kanpur
Topics
Character Arrays and String Handling, Structures
Dynamic Memory Allocation, Stack & Link Lists
Macros & C Processor, File Handling
_______________________________________________________________
Character Arrays and String Handling
35. WAP to find the length of a string without using library function.
#include<stdio.h>
#include<conio.h>
void main()
{
char string[50];
int i , length = 0;
printf (“Enter a string\n”);
gets (string);
for (i=0; string[i] != ‘\0’; i++) /*keep going through each */
{ /*character of the string */
length++; /*till its end */
}
printf(“The length of a string is the number of characters in it\n”);
printf(“So, the length of %s =%d\n”, string, length);
}
/*—————————————————-
Output
Enter a string
hello
The length of a string is the number of characters in it
So, the length of hello = 5
RUN2
Enter a string
E-Commerce is hot now
The length of a string is the number of characters in it
So, the length of E-Commerce is hot now =21
36. Progranm to compare two strings using user defined function. If strings are identical display “The Two Strings are Identical” otherwise the strings are different.
#include<stdio.h>
#include<conio.h>
void main()
{
int count1=0,count2=0,flag=0,i;
char str1[10],str2[10];
clrscr();
puts(“Enter a string:”);
gets(str1);
puts(“Enter another string:”);
gets(str2);
/*Count the number of characters in str1*/
while (str1[count1]!=’\0′)
count1++;
/*Count the number of characters in str2*/
while (str2[count2]!=’\0′)
count2++;
i=0 ;
while ( (i < count1) && (i < count2))
{ if (str1[i] == str2[i])
{ i++;
continue ;
}
if (str1[i]<str2[i])
{ flag = -1;
break;
}
if (str1[i] > str2[i])
{ flag = 1;
break;
}
}
if (flag==0)
printf(“Both strings are equal\n”);
if (flag==1)
printf(“String1 is greater than string2\n”, str1, str2);
if (flag == -1)
printf(“String1 is less than string2\n”, str1, str2);
getch(); }
/*—————————————-
Output
Enter a string:
happy
Enter another string:
HAPPY
String1 is greater than string2
RUN2
Enter a string:
Hello
Enter another string:
Hello
Both strings are equal
RUN3
Enter a string:
gold
Enter another string:
silver
String1 is less than string2
—————————————-*/
37. WAP to know whether the entered character string is Palindrome or not.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], revString[25]={‘\0’};
int i,length = 0, flag = 0;
clrscr();
fflush(stdin);
printf(“Enter a string\n”);
gets(string);
for (i=0; string[i] != ‘\0’; i++) /*keep going through each */
{ /*character of the string */
length++; /*till its end */
}
printf(“The length of the string \’%s\’ = %d\n”, string, length);
for (i=length-1; i >= 0 ; i–)
{ revString [length-i-1] = string[i]; }
/*Compare the input string and its reverse. If both are equal then the input string is palindrome.
Otherwise it is not a palindrome */
for (i=0; i < length ; i++)
{
if (revString[i] = = string[i])
flag = 1;
else
flag = 0;
}
if (flag = = 1)
printf (“%s is a palindrome\n”, string);
else
printf(“%s is not a palindrome\n”, string);
38. Write a C program read a sentence and count the number of vowels and consonants in the given sentence.
#include <stdio.h>
#include <conio.h>
void main()
{
char sentence[80];
int i, vowels=0, consonants=0, special = 0;
clrscr();
printf(“Enter a sentence\n”);
gets(sentence);
for(i=0; sentence[i] != ‘\0’; i++)
{
if((sentence[i] == ‘a’||sentence[i] == ‘e’||sentence[i] == ‘i’||
sentence[i] == ‘o’||sentence[i] == ‘u’) ||(sentence[i] == ‘A’||
sentence[i] == ‘E’||sentence[i] == ‘I’|| sentence[i] == ‘O’||
sentence[i] == ‘U’))
{ vowels = vowels + 1; }
else
{ consonants = consonants + 1; }
if (sentence[i] ==’\t’ ||sentence[i] ==’\0′ || sentence[i] ==’ ‘)
{ special = special + 1; }
}
consonants = consonants – special;
printf(“No. of vowels in %s = %d\n”, sentence, vowels);
printf(“No. of consonants in %s = %d\n”, sentence, consonants);
}
/*—————————————-
Output
Enter a sentence
Good Morning
No. of vowels in Good Morning = 4
No. of consonants in Good Morning = 7 */
Structures
39. Program to copy structure elements from one object to another object.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
struct cd_disk
{
char co [15] ;
float type ;
int price ;
} ;
struct disk d1 = {“SONY” , 1.44 , 20};
struct disk d2 , d3 ;
strcpy (d2.co,d1.co) ;
d2.type=d1.type ;
d2.price = d1.price;
d3=d2 ;
printf(“\n %s %f %d”,d1.co , d1.type ,d1.price) ;
printf(“\n %s %f %d”,d2.co , d2.type ,d2.price) ;
printf(“\n %s %f %d”,d3.co , d3.type ,d3.price) ;
}
40. Program to create array of structure objects.
#include <stdio.h>
#include <conio.h>
void main()
{
struct stud
{
char name [12] ;
int rollno ;
char grade [2] ;
} ;
struct stud st [4] ;
printf(“Enter the data for each student\n”)
for (i=0 ; i<=3 ;i++)
{ scanf(“%s %d %s”, &st[i].name, &st[i].rollno , &st[i].grade) ; } // reading structure elements
printf(“Record of students are as follows:\n”) ;
for (i=0 ; i<=3 ;i++)
{ printf (“%s %d %s\n”, st[i].name, st[i].rollno , st[i].grade) ; } // printing structure elements
getch();
} // End of main()
41. Program to declare pointer to structure and display the contents of the structure.
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{
char name [20] ;
char author [25] ;
int pages ;
} ;
struct book b1={“Ethical Hacking”, “Ankit Fadia” , 1093};
struct book *ptr ;
ptr=&b1 ;
clrscr();
printf (“\n%s by %s of %d pages”, b1.name, b1.author, b1.pages) ; // printing structure elements
printf (“\n%s by %s of %d pages”, ptr ->name,ptr->author, ptr->pages) ; // use of arrow operator
printf (“\n%s by %s of %d pages”, (*ptr ).name, (*ptr).author, (*ptr).pages) ; // use of dot operator.
getch();
} // End of main ()
Dynamic Memory Allocation, Stack & Link Lists
42.Write a C program to find the sum of two one-dimensional arrays using Dynamic Memory Allocation
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
void main()
{
int i,n;
int *a,*b,*c;
printf(“How many Elements in each array…\n”);
scanf(“%d”, &n);
a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c =( int *) malloc(n*sizeof(int));
printf(“Enter Elements of First List\n”);
for(i=0;i<n;i++)
{ scanf(“%d”,a+i); }
printf(“Enter Elements of Second List\n”);
for(i=0;i<n;i++)
{ scanf(“%d”,b+i); }
for(i=0;i<n;i++)
{ *(c+i) = *(a+i) + *(b+i); }
printf(“Resultant List is\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,*(c+i));
}
} /* End of main() */
/*—————————————
Output
How many Elements in each array…4
Enter Elements of First List
1 2 3 4
Enter Elements of Second List
6 7 8 9
Resultant List is 7 9 11 13 —————————————-*/
43. Write a C program to read N integers and store them in an array A, and so find the sum of all these elements using pointer. Output the given array and the computed sum with suitable heading.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
void main()
{
int i,n,sum=0;
int *a;
clrscr();
printf(“Enter the size of array A\n”);
scanf(“%d”, &n);
a=(int *) malloc(n*sizeof(int)); /*Dynamic Memory Allocation */
printf(“Enter Elements of First List\n”);
for(i=0;i<n;i++)
{ scanf(“%d”,a+i); }
/*Compute the sum of all elements in the given array*/
for(i=0;i<n;i++)
{ sum = sum + *(a+i); }
printf(“Sum of all elements in array = %d\n”, sum);
} /* End of main() */
44. Write a C program to implement stack. Stack is a LIFO data structure LIFO – Last in First Out Perform PUSH (insert operation), POP (Delete operation) and Display stack.
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5
struct stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display (void);
void main ()
{
int choice;
int option = 1;
clrscr ();
s.top = -1;
printf (“STACK OPERATION\n”);
while (option)
{
printf (“——————————————\n”);
printf (” 1 –> PUSH \n”);
printf (” 2 –> POP \n”);
printf (” 3 –> DISPLAY \n”);
printf (” 4 –> EXIT \n”);
printf (“——————————————\n”);
printf (“Enter your choice\n”);
scanf (“%d”, &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}
fflush (stdin);
printf (“Do you want to continue(Type 0 or 1)?\n”);
scanf (“%d”, &option);
} }
/*Function to add an element to the stack*/
void push ()
{ int num;
if (s.top == (MAXSIZE – 1))
{ printf (“Stack is Full\n”);
return;
}
else
{ printf (“Enter the element to be pushed\n”);
scanf (“%d”, &num);
s.top = s.top + 1;
s.stk[s.top] = num;
} return; }
/*Function to delete an element from the stack*/
int pop ()
{ int num;
if (s.top == – 1)
{ printf (“Stack is Empty\n”);
return (s.top);
}
else
{ num = s.stk[s.top];
printf (“poped element is = %d\n”, s.stk[s.top]);
s.top = s.top – 1;
} return(num); }
/*Function to display the status of the stack*/
void display ()
{ int i;
if (s.top = = -1)
{ printf (“Stack is empty\n”);
return ;
}
else
{ printf (“\nThe status of the stack is\n”);
for (i = s. top; i >= 0; i–)
{ printf (“%d\n”, s.stk[i]) ; }
}
printf (“\n”); }
Macros & C Processor
45. Program in C using Macros to calculate the area of a circle where value of PI is preprocessed.
.#include<conio.h>
#include<stdio.h>
#define PI 3.14
#define area(x) PI*x*x
void main()
{
float rad , A=0.0 ;
clrscr ();
printf (“\n Enter the radius of circle:”) ;
scanf (“%f”, &rad ) ;
A = area( rad ) ;
printf (“\n Area of circle =%f”, A) ;
getch() ;
}
RUN
Enter the radius of circle: 4.5 Area of circle =63.584999
File Handling
46. Write a C program to create a file called emp.rec and store information about a person, in terms of his name, age and salary.
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
fptr = fopen (“emp.rec”, “w”); /*open for writing*/
if (fptr == NULL)
{
printf (“File does not exists\n”);
return ;
}
printf(“Enter the name\n”);
scanf(“%s”, name);
fprintf(fptr, “Name = %s\n”, name);
printf(“Enter the age\n”);
scanf(“%d”, &age);
fprintf(fptr, “Age = %d\n”, age);
printf (“Enter the salary\n”);
scanf(“%f”, &salary);
fprintf( fptr , “Salary = %.2f\n”, salary) ;
fclose(fptr);
}
/*—————————————————————————
Output
Enter the name
Prabhu
Enter the age
25
Enter the salary
25000
————————————-
Please note that you have to open the file called emp.rec in the directory