(b) Find the year in which minimum and maximum export was made
Chapter 8: Strings and Standard Functions
1. Write a program to arrange a set of fruit names given below in descending order (reverse alphabetic) (mango, banana, apple, orange, graphs, coconut, water melon and papaya).
#include<stdio.h>
void main() {
char
str[8][15]={"mango","banana","apple","orange","graphs","coconut","water melon","papaya"};
char temp[15];
int i,j;
for(i=0;i<7;i++) {
for(j=i;j<8;j++) {
if(str[i][0]<str[j][0]) {
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
} }
printf("The given names of fruits in sorted order are:\n");
for(i=0;i<8;i++)
printf("%s\n",str[i]);
}
Output:
The given names of fruits in sorted order are:
water melon papaya orange mango graphs coconut banana apple
2. Write a program to arrange the following names in the alphabetic order. The sorting is to be done on the first three characters of the first name. (Ashok, Alok , Akash, Amit, Amol, Anil, Ashish and Anand).
#include<stdio.h>
void main() {
char
name[8][15]={"Ashok","Alok","Akash","Amit","Amol","Anil","Ashish","Anan d"},temp[15];
int i,j;
for(i=0;i<7;i++) {
for(j=i;j<8;j++) {
if((name[i][0]>name[j][0])||((name[i][0]==name[j][0])&&(name[i][1 ]>name[j][1]))||((name[i][0]==name[j][0])&&(name[i][1]==name[j][1])&&(n ame[i][2]>name[j][2])))
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
} }
}
printf("The given names in sorted order are:\n");
for(i=0;i<8;i++)
printf("%s\n",name[i]);
}
Output:-
Akash Alok Amit Amol Anand Anil Ashish Ashok
3. Write a program to enter some text and display the text in reverse order. (Example: ‘I am happy’ will be displayed as ‘happy am I’).
#include<stdio.h>
void main() {
char text[80];
int i,n,f,r;
printf("Enter the text\n");
scanf("%[^\n]",text);
n=strlen(text);
r=n-1;
while(text[r]==' ') r--;
f=r;
while(f>=0) {
while(text[f]!=' '&& f>=0)
By Prof. Ashok N. Kamthane f--;
for(i=f+1;i<=r;i++)
printf("%c",text[i]);
printf(" ");
r=f;
while(text[r]==' '&& r>=0) r--;
f=r;
}
printf("\n");
}
Output:
Enter the text I am happy happy am I
3. Write a program to enter five full names of persons. Display their names, initials and last names.
#include<stdio.h>
void main() {
char name[5][3][10];
int i,f,r,n;
printf("Enter full names of five persons in the form First_name Middle_Name Last_Name,enter each name on new line\n");
for(i=0;i<5;i++) {
scanf("%s %s %s",name[i][0],name[i][1],name[i][2]);
}
printf("THe names with initials are\n");
for(i=0;i<5;i++) {
printf("%c. %c.
%s\n",name[i][0][0],name[i][1][0],name[i][2]);
} }
Output:
Enter full names of five persons in the form First_name Middle_Name Last_Name,enter each name on new line
Shrikant S Polawar Krishna P Arutwar Suraj H Nilapalle Harshad H Surana Ashwin V Bagde
THe names with initials are S. S. Polawar
K. P. Arutwar S. H. Nilapalle H. H. Surana A. V. Bagde
4. Write a program to enter text through keyboard. Convert first character of each word in capital and display the text.
#include<stdio.h>
void main() {
char text[80];
int f,n;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(f<n) {
while(text[f]==' ' && f<n) f++;
if(text[f]>=97 && text[f]<=122) text[f]-=32;
while(text[f]!=' '&&f<n) f++;
}
printf("%s\n",text);
}
Output:
Enter the text at the end press enter i love my country
I Love My Country
6. Write a program to enter some text through the keyboard. Insert dot (.) after every three words in the text. The first character after every dot should be converted to capital.
#include<stdio.h>
void main() {
char text[80];
int f,n,count=0;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(text[f]==' ' && f<n) f++;
if(text[f]>=97 && text[f]<=122) text[f]-=32;
By Prof. Ashok N. Kamthane while(f<n)
{
while(text[f]!=' ' && f<n) f++;
count++;
while(text[f]==' ' && f<n) //Removing the extra spaces between two words
f++;
if(count%3==0) {
if(text[f]>=97 && text[f]<=122) text[f]-=32;
} }
printf("Text aftre processing :\n%s",text);
}
Output:
Enter the text at the end press enter
c is one of the middle level languages, it is compiled language.
Text aftre processing :
C is one Of the middle Level languages, it Is compiled language.
7. Write a program to enter some text through the keyboard. Count the number of words that starts from ‘w’. Display such words and count them.
#include<stdio.h>
void main() {
char text[80];
int f,n,count;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(f<n) {
while(text[f]==' ' && f<n) f++;
if(text[f]=='w'||text[f]=='W') count++;
while(text[f]!=' '&&f<n) f++;
}
printf("No. of words starting with w is :%d\n",count);
}
Output:
Enter the text at the end press enter
WHO that is world health organisation is the one which works for social purpose No. of words starting with w is :4
9. Write a program to encrypt the text ‘INDIA’.The output should be ‘KPFKC’. (‘A’ is to be replaced with ‘C’, ‘B’ with ‘D’ and ‘C’ with ‘E’ and so on.)
#include<stdio.h>
void main() {
char str[]="INDIA";
int n,i;
n=strlen(str);
for(i=0;i<n;i++) str[i]+=2;
printf("Text after encryption\t:%s",str);
}
Output:
Text after encryption :KPFKC
10. Write a program to dycrypt the text ‘KPFKC’ to get original string ‘INDIA’.
#include<stdio.h>
void main() {
char str[]="KPFKC";
int i,n;
printf("Decryting text using the encryption key used in above algorithm\n");
printf("Text after decryption is:");
n=strlen(str);
for(i=0;i<n;i++) {
str[i]=str[i]-2;
}
printf("%s",str);
}
Output:
Decryting text using the encryption key used in above algorithm Text after decryption is:INDIA
By Prof. Ashok N. Kamthane
Chapter 9: Pointers
1. Write a program to accept a string using character pointer and display it.
#include<stdio.h>
#include<conio.h>
void main() {
char *ch;
clrscr();
printf("Enter the string : ");
scanf("%s",ch);
printf("\n\n\t\tYou entered : %s",ch);
getch();
}
Output:
Enter the string : Ashok
You entered : Ashok
2. Write a program to calculate square and cube of the entered number using pointer of the variable containing entered number.
#include<stdio.h>
#include<conio.h>
void main() {
int a,*p;
clrscr();
printf("Enter any number : ");
scanf("%d",&a);
p=&a;
printf("\n\nSquare of %d : %d\n\n Cube of %d : %d\n ", *p, *p**p,
*p, *p**p**p);
getch();
}
3. Write a program to display all the elements of an array using pointer.
#include<stdio.h>
#include<conio.h>
void main() {
int arr[4]={1,5,8,3},*p,i;
clrscr();
p=arr;
printf("Showing array elements using pointer...\n");
for(i=0;i<4;i++)
printf("arr[%d] : %d\n",i,*p++);
getch();
}
Output:
Showing array elements using pointer...
arr[0] : 1 arr[1] : 5 arr[2] : 8 arr[3] : 3
4. Write a program to allocate memory for 10 integers.
#include <stdio.h>
#include <alloc.h>
#include <process.h>
void main() {
int *ptr,i;
clrscr();
if ((ptr = (int *) malloc(10)) == ‘\0’) {
printf("Not enough memory...\n");
exit(1);
}
else printf("Memory allocation successful for 10 integers..\n");
printf("Enter 10 integers..\n");
for(i=0;i<10;i++) scanf("%d",&ptr[i]);
printf("\nEntered 10 integers are..\n");
for(i=0;i<10;i++) printf("%d\t",ptr[i]);
free(ptr);
getch();
}
Output:
Memory allocation successful for 10 integers..
Enter 10 integers..
2 4 6 8 0 1 3 5 7 9
Entered 10 integers are..
2 4 6 8 0 1 3 5 7 9
5. Write a program to demonstrate the use of
realloc().
#include <stdio.h>
#include <alloc.h>
By Prof. Ashok N. Kamthane
#include <process.h>
void main() {
char *str;
clrscr();
str = (char *) malloc(7);
strcpy(str, "Hello");
printf("String is %s\n Address is %u\n", str, str);
str = (char *) realloc(str,3);
strcpy(str, "Hi");
printf("New String is %s\n New address is %d\n", str, str);
free(str);
getch();
}
Output:
String is Hello Address is 2342 New String is Hi New address is 2358
Chapter 10: Functions
1. Write a program to display three different Metro names of India by using different functions.
#include<stdio.h>
void del();
void kol();
void mum();
void main() {
del();
mum();
kol();
}
void del() {
printf("DELHI\n");
}
void mum() {
printf("MUMBAI\n");
}
void kol() {
printf("KOLKATA\n");
}
Output:
DELHI MUMBAI KOLKATA
2. Write a program with two functions and call one function from other.
#include<stdio.h>
void fun1();
void fun2();
void main() {
printf("Calling fun1() from main()\n");
fun1();
printf("We are back in main now\n");
}
void fun1() {
printf("We are in fun1()\n");
printf("Calling fun2() from fun1()...\n");
fun2();
printf("We are again in fun1()\n");
By Prof. Ashok N. Kamthane }
void fun2() {
printf("We are in fun2 now\n");
}
Output:
Calling fun1() from main() We are in fun1()
Calling fun2() from fun1()...
We are in fun2 now We are again in fun1() We are back in main now
3. Write a program which takes an int argument and calculates its cube.
#include<stdio.h>
int xcube(int x)
{ return(x*x*x);
}
void main() {
int n;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Cube of %d is %d\n",n,xcube(n));
}
Output:
Enter a no.
3
Cube of 3 is 27
4. Write a program to display the table of given number. Write different functions for accepting the input, calculating the table and displaying the value.
#include<stdio.h>
int input();
void cal(int,int *);
void display(int *);
void main() {
int n,tab[10];
n=input();
cal(n,tab);
display(tab);
}
int input() {
int n;
printf("Enter a no.\n");
scanf("%d",&n);
return n;
}
void cal(int n ,int *tab) {
int i;
for(i=1;i<=10;i++) *(tab+i)=n*i;
}
void display(int *tab) {
int i;
printf("Multiplication table of %d is as follows\n",*(tab+1));
for(i=1;i<=10;i++) {
printf("%d\n",*(tab+i));
} }
Output:
Enter a no.
5
Multiplication table of 5 is as follows 5
10 15 20 25 30 35 40 45 50
5. Write a program to calculate the sum of digits of a number. Use a function to return the sum.
#include<stdio.h>
int calsum(int);
void main() {
int n;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Sum of digits of no. %d is %d \n",n,calsum(n));
}
int calsum(int n) {
int t=0;
By Prof. Ashok N. Kamthane while(n)
{
t=t+n%10;
n=n/10;
}
return t;
}
Output:
Enter a no.
234
Sum of digits of no. 234 is 9
6. Write a program to swap the two variables present in one function to other function.
#include<stdio.h>
void swap(int *,int *);
void fun();
void main()
{ fun();
}
void fun() {
int a,b;
printf("Enter two no.s\n");
scanf("%d%d",&a,&b);
printf("Before calling swap()\na=%d\tb=%d",a,b);
swap(&a,&b);
printf("\nAfter calling swap()\na=%d\tb=%d",a,b);
}
void swap(int *a,int *b) {
int temp=*a;
*a=*b;
*b=temp;
}
Output:
Enter two no.s 10
20
Before calling swap() a=10 b=20
After calling swap() a=20 b=10
7. Write a program to sort an array (in descending order) in different function and return it to the original function.
#include<stdio.h>
void sort(int ,int *);
void main() {
int arr[10],n,i;
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
sort(n,arr);
printf("Array after sorting is\n");
for(i=0;i<n;i++) {
printf("%d\t",arr[i]);
} }
void sort(int num,int *array) {
int i,j,t;
for(i=0;i<num-1;i++) {
for(j=i;j<num;j++) {
if(array[i]>array[j]) {
t=array[i];
array[i]=array[j];
array[j]=t;
} }
} }
Output:
Enter size of array 5
Enter the array elements 9 2 8 3 0
Array after sorting is
0 2 3 8 9
8. Write a program to display ‘Hello!’ five times. Create a user-defined function
message()
.
#include<stdio.h>
void message() {
int i;
for(i=0;i<5;i++)
printf("Hello!\n");
}
void main() {
By Prof. Ashok N. Kamthane message();
}
Output:
Hello!
Hello!
Hello!
Hello!
Hello!
9. Write a program to calculate average marks of five subjects by using pass by value.
#include<stdio.h>
float avg(int *);
void main() {
int arr[5],i;
float a;
printf("Enter the marks of five subjects\n");
for(i=0;i<5;i++) {
scanf("%d",&arr[i]);
}
a=avg(arr);
printf("Average of the marks of five subjects is :%f",a);
}
float avg(int *arr) {
int t=0,i;
for(i=0;i<5;i++) t=t+*(arr+i);
return(t/5.0);
}
Output:
Enter the marks of five subjects 69 74 83 78 82
Average of the marks of five subjects is :77.199997