C Program10
C Program10
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];
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++)
h[pre-'a'].count++;
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];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf(" %[^\n]",f[i].name);
scanf("%lf",&f[i].number);
scanf(" %[^\n]",search);
for(int i=0;i<n;i++)
if(stricmp(f[i].name,search)==0)
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];
}f[50];
int main()
int n,k=0;
char t[50];
scanf("%d",&n);
for(int i=0;i<n;i++)
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;
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];
scanf("%[^\n]s",a);
for(int i=0;i<strlen(a);i++)
s.digits++;
if(a[i]==' ')
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);
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,
#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;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i].name);
scanf("%d",&s[i].roll);
scanf("%d",&s[i].phy1);
scanf("%d",&s[i].phy2);
scanf("%d",&s[i].chem1);
scanf("%d",&s[i].chem2);
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);
,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;
scanf("%d",&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;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&s[i].rollnum);
scanf("%s",&s[i].day);
scanf("%s",&s[i].month);
printf("Enter the year of birth : ");
scanf("%d",&s[i].year);
scanf("%d",&search);
for(int i=0;i<n;i++)
if(s[i].year==search)
count++;
if(count>0)
if(count == 0)
#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()
{
strcpy(std2.name,std1.name);
std2.rollnum=std1.rollnum;
strcpy(std2.department,std1.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
#include<stdio.h>
int main()
int a[20],n,t,oddindex=0;
scanf("%d",&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
#include<stdio.h>
struct date
char day[50];
int month;
int year;
}DOB;
struct employee
char name[50];
char gender[50];
char designation[50];
char department[50];
int salary;
}e;
int main()
scanf(" %[^\n]",&e.name);
scanf("%s",&e.DOB.day);
scanf("%d",&e.DOB.month);
scanf("%d",&e.DOB.year);
scanf(" %[^\n]",&e.gender);
scanf(" %[^\n]",&e.designation);
scanf(" %[^\n]",&e.department);
scanf("%d",&e.salary);
#include<stdio.h>
#include<string.h>
int main()
char a[100],b[50][50];
int frequency[50]={0},words=0;
scanf("%[^\n]s",&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]);