You are on page 1of 14

ASSIGNMENT 10

1.Create a program that determines and displays the number of times, such that
characters appears in a given sentence (histogram) entered by the user. Use a structure to
solve this problem.
#include<stdio.h>

struct histogram

char letters;

int count;

}h[26];

int main()

char a[100];

printf("Enter the sentence \n");

scanf("%[^\n]s",a);

for(int i=0;i<26;i++)

h[i].letters='a'+i;

h[i].count=0;

for(int i=0;a[i];i++)

char pre = a[i];

if('a'<=pre && pre<='z')

h[pre-'a'].count++;

else if('A'<=pre && pre<='Z')

h[pre-'A'].count++;

}
for(int i=0;i<26;i++)

if(h[i].count>0)

printf("%c : %d\n",h[i].letters,h[i].count);

2. Write a program which takes as input your friends names, their phone number and stores them
in a structure Perform the following operations on the structure.

i) Search for a given friend and print his details if present in the structure and otherwise, print not
found.

#include<stdio.h>

#include<string.h>

struct friends

char name[50];

float number;

}f[50];

int main()

int n,found=1;

char search[50];

printf("Enter the number of friends \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name %d : ",i+1);

scanf(" %[^\n]",f[i].name);

printf("Enter the number %d : ",i+1);

scanf("%lf",&f[i].number);

printf("Enter the friend to be searched : ");

scanf(" %[^\n]",search);
for(int i=0;i<n;i++)

if(stricmp(f[i].name,search)==0)

printf("Name : %s Phone number : %lf",f[i].name,f[i].number);

found = 1;

break;

if(i==n-1)

printf("Not found");

ii)Sort the structure in alphabetical order of names and as well using the phone numbers.

#include<stdio.h>

#include<string.h>

struct friends

char name[50];

long int number;

}f[50];

int main()

int n,k=0;

long int temp;

char t[50];

printf("Enter the number of friends \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name %d : ",i+1);


scanf("%s",f[i].name);

printf("Enter the number %d : ",i+1);

scanf("%ld",&f[i].number);

for(int i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(strcmp(f[i].name , f[j].name)>0)

strcpy(t,f[i].name);

strcpy(f[i].name,f[j].name);

strcpy(f[j].name,t);

temp=f[i].number;

f[i].number=f[j].number;

f[j].number=temp;

printf("%s : %ld \n",f[i].name,f[i].number);

3. Write a C program that accepts a sentence from the user and displays the sentence statistics by
creating a structure.

#include<stdio.h>

#include<string.h>

struct statistics

int words;

int vowels;
int digits;

};

int main()

char a[100];

printf("Enter the string \n");

scanf("%[^\n]s",a);

struct statistics s = {1,0,0};

for(int i=0;i<strlen(a);i++)

if(a[i]>='0' && a[i]<='9')

s.digits++;

if(a[i]==' ')

if(!(a[i+1]>='0' && a[i+1]<='9') && !(a[i-1]>='0'&&a[i-1]<='9'))

s.words++;

if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||
a[i]=='U')

s.vowels++;

printf("The number of words : %d\nThe number of vowels : %d\nThe number of digits : %d\
n",s.words,s.vowels,s.digits);

4. Write a program to compute the rank of n number of students in a class based on

total marks obtained. Input details include Roll Number, Name, Mathematics Mark,

Physics Mark and Chemistry mark for two assessments. Design a suitable structure

and output should include input details along with total mark,

computed rank and serial number.

#include<stdio.h>
#include<string.h>

struct student

int roll;

char name[100];

int phy1,phy2;

int chem1,chem2;

int math1,math2;

int tot;

}s[50];

int main()

char z[100];

int n,t;

printf("Enter the number of students \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name of %d student : ",i+1);

scanf("%s",s[i].name);

printf("Enter the rollnum of student : ");

scanf("%d",&s[i].roll);

printf("Enter the physics mark 1 : ");

scanf("%d",&s[i].phy1);

printf("Enter the physics mark 2 : ");

scanf("%d",&s[i].phy2);

printf("Enter the chemistry mark 1 : ");

scanf("%d",&s[i].chem1);

printf("Enter the chemistry mark 2 : ");

scanf("%d",&s[i].chem2);

printf("Enter the maths mark 1 : ");


scanf("%d",&s[i].math1);

printf("Enter the maths mark 2 : ");

scanf("%d",&s[i].math2);

s[i].tot=s[i].math1+s[i].math2+s[i].phy1+s[i].phy2+s[i].chem1+s[i].chem2;

for(int i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(s[i].tot<s[j].tot)

t=s[i].tot;

s[i].tot=s[j].tot;

s[j].tot=t;

t=s[i].roll;

s[i].roll=s[j].roll;

s[j].roll=t;

t=s[i].math1;

s[i].math1=s[j].math1;

s[j].math1=t;

t=s[i].math2;

s[i].math2=s[j].math2;

s[j].math2=t;

t=s[i].phy1;

s[i].phy1=s[j].phy1;

s[j].phy1=t;
t=s[i].phy2;

s[i].phy2=s[j].phy2;

s[j].phy2=t;

t=s[i].chem1;

s[i].chem1=s[j].chem1;

s[j].chem1=t;

t=s[i].chem2;

s[i].chem2=s[j].chem2;

s[j].chem2=t;

strcpy(z,s[i].name);

strcpy(s[i].name,s[j].name);

strcpy(s[j].name,z);

printf("%d : %s\t%d rank\ttotal : %d phy1 : %d phy2 : %d chem1 : %d chem2 : %d math1 : %d


math2 : %d\n\n "

,s[i].roll,s[i].name,i+1,s[i].tot,s[i].phy1,s[i].phy2,s[i].chem1,s[i].chem2,s[i].math1,s[i].math2);

5. Write a program using function to calculate the sum of all numbers between 1 and n.

#include<stdio.h>

int sum(int n)

int sum=0;

for(int i=1;i<=n;i++)

sum=sum+i;

return sum;

}
int main()

int n;

printf("Enter the number of terms \n");

scanf("%d",&n);

printf("The sum is %d",sum(n));

6. Declare an array of structures, where each structure has student register number, day , month
and year of birth. Get input from the user for a class of 15 students. Write a completer c program
to find the number of students who were born in a given years.

#include<stdio.h>

struct student

int rollnum;

char day;

char month;

int year;

}s[50];

int main()

int n,search,count=0;

printf("Enter the number of students \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the register number %d : ",i+1);

scanf("%d",&s[i].rollnum);

printf("Enter the day of birth : ");

scanf("%s",&s[i].day);

printf("Enter the month of birth : ");

scanf("%s",&s[i].month);
printf("Enter the year of birth : ");

scanf("%d",&s[i].year);

printf("Enter the required year : ");

scanf("%d",&search);

for(int i=0;i<n;i++)

if(s[i].year==search)

count++;

if(count>0)

printf("%d students were born in %d year",count,search);

if(count == 0)

printf("%d year is not found",search);

7. Write a C program to demonstrate copying one structure to another of same type.

#include<stdio.h>

#include<string.h>

struct student

char name[50];

int rollnum;

char department[50];

}std1;

struct copy

char name[50];

int rollnum;

char department[50];

}std2;

int main()
{

struct student std1 = {"luffy ",ECE,"Batch I"};

strcpy(std2.name,std1.name);

std2.rollnum=std1.rollnum;

strcpy(std2.department,std1.department);

printf("The copied structure is Name : %s Rollnum : %d Department :


%s",std2.name,std2.rollnum,std2.department);

8. Write a program to arrange elements in the list in such a way that all odd elements

appear first and all even elements appear at the end of the array. No additional

array must be used. Odd and even values should maintain their relative order within

each group as in original array.

#include<stdio.h>

int main()

int a[20],n,t,oddindex=0;

printf("Enter the number of elements to be in the array \n");

scanf("%d",&n);

printf("Enter the elements \n");

for(int i=0;i<n;i++)

scanf("%d",&a[i]);

for(int i=0;i<n;i++)

if(a[i]%2 == 1)

t=a[i];

for(int j=i;j>oddindex;j--)

a[j]=a[j-1];

a[oddindex]=t;

oddindex++;

}
}

for(int i=0;i<n;i++)

printf("%d ",a[i]);

9. Write a program that stores (in a structure) and then displays information about an

employee such as name, DOB, gender, designation, department, and salary. The

date of birth (DOB) attribute of an employee should be stored as a nested structure

with day, month and year attributes

#include<stdio.h>

struct date

char day[50];

int month;

int year;

}DOB;

struct employee

char name[50];

struct date DOB;

char gender[50];

char designation[50];

char department[50];

int salary;

}e;

int main()

printf("Enter the name of the employee : ");

scanf(" %[^\n]",&e.name);

printf("Enter the DOB of the employee : ");

scanf("%s",&e.DOB.day);
scanf("%d",&e.DOB.month);

scanf("%d",&e.DOB.year);

printf("Enter the gender of the employee : ");

scanf(" %[^\n]",&e.gender);

printf("Enter the designation of the employee : ");

scanf(" %[^\n]",&e.designation);

printf("Enter the department of the employee : ");

scanf(" %[^\n]",&e.department);

printf("Enter the salary of the employee : ");

scanf("%d",&e.salary);

printf("Name : %s DOB : %s %d %d Gender : %s Designation : %s Department : %s Salary :


%d",e.name,e.DOB.day,e.DOB.month,e.DOB.year,e.gender,e.designation,e.department,e.salary);

10. Write a program to print the frequency of each word of input.

#include<stdio.h>

#include<string.h>

int main()

char a[100],b[50][50];

int frequency[50]={0},words=0;

printf("Enter the string \n");

scanf("%[^\n]s",&a);

char *token = strtok(a," ");

while(token != NULL)

strcpy(b[words],token);

frequency[words]++;

words++;

token=strtok(NULL," ");

for(int i=0;i<words;i++)
{

for(int j=i+1;j<words;j++)

if(stricmp(b[i],b[j])==0)

frequency[i]++;

frequency[j]=0;

if(frequency[i]!=0)

printf("%s : %d\n",b[i],frequency[i]);

You might also like