Friday, November 21, 2014

C Array Structure



C Program to Sort array of Structure

#include
#include
struct state
{
char name[50];
long int population;
float literacyrate;
float income;
}
st[m];
int main()
{
int i,n,m1,mi,maximumliteracyrate,maximumincome;
float rate;
m1=mi=-1;
maximumliteracyrate=maximumincome=0;
printf("Enter how many states: ");
scanf("%d",&n);
for(i=0;i
{
printf("\nEnter state %d details: ",i);
printf("\n Enter state name:”);
scanf(“%s",&st[i].name);
printf("\n Enter total population:”);
scanf("%ld",&st[i].population);
printf("\n Enter total literary rate: ");
scanf("%f",&rate);
st[i].literarcyrate =rate;
printf("\n Enter total income: ");
scanf("%f",&st[i].income);
}
for(i=0;i
{
if(st[i].literacyrate>=maximumliteracyrate)
{
maximumliteracyrate=.st[i].literacyrate;
m1++;
}
if(st[i].income>maximumincome)
{
maximumincome=st[i].income;
mi++;
}
}
printf("\n State with highest literary rate :%s",st[m1].name);
printf("\n State with highest income :%s",st[mi].name);
return(0);
}
Output
                   Enter how many state : 3
Enter state 0 details:
Enter state name : Maharashtra
Enter total population : 1000
Enter total literary rate : 90
Enter total income : 10000
Enter state 1 details:
Enter state name : Goa
Enter total population : 300
Enter total literary rate : 94
Enter total income : 5000
Enter state 2 details:
Enter state name : Gujrat
Enter total population : 900
Enter total literary rate : 78
Enter total income : 7000
          The state whose literary rate is the highest : Goa
          The state whose income is the highest: Maharashtra

Find Substring of String without Using Library Function

#include
#include
int search(char[],char[]);
int main()
{
int loc;
char source[]="Maharashtra";
char target[]="sht";
loc=search(source,target);
if(loc==-1)
printf("\n Not found");
else
printf("\n Found at location %d",loc+1);
return(0);
}
int search (char src[], char str[])
{
int i,j,firstocc;
i=0,j=0;
while(src[i]!=’\0’)
{
while(src[i]!=str[0]&&src[i]!=’\0’)
i++;
if(src[i]==’\0’)
return(-1);
firstocc=i;
while(src[i]==str[j]&&src[i]!=’\0’&&str[j]!=’\0\)
{
i++;
j++;
}
if(str[j]==’\0’)
return(firstocc);
if(src[i]==’\0’)
return(-1)
i=firstocc+1;
j=0;
}
}

No comments:

Post a Comment