Friday, November 21, 2014

C String using pointers



C Program to read, print name and other details of 50 students

 #include
#include
#dfine Size 50
struct student
{
char name[30];
int rollno;
int sub[3];
};
void main()
{
int i,j,max,count,total,n,a[Size],ni;
struct student st[Size];
clrscr();
printf("Enter how many students: ");
scanf("%d",&n);
for(i=0;i
{
printf("\n Enter name and roll number for student %d"i);
scanf("%s",st[i].name);
scanf("%d",&st[i].rollno);
}
}
for(i=0;i
{
total=0;
for(j=0;j<3 j="" span="">
{
total=total+st[i].sub[j];
}
printf("\n Total marks obtained by student %s are %d\n”, sti].name,total);
a[i].total;
}
for(j=0;j<3 j="" span="">
{
max=0;
for(i=0;i
{
if(max
{
max=st[i].sub[j];
ni=i;
}
}
printf("\n Student %s got maximum marks = %d in subject :%d",st[ni].name,max,j);
}
max=0;
for(i=0;i
{
if(max
{
max=a[i];
ni=I;
}
}
printf("\n%s Obtained the total highest marks. ",st[ni].name);
getch();
}

Output

Enter how many students: 2
Enter name and roll number for student 0: Pritesh 1
Enter name and roll number for student 1: Suraj 2
Enter marks of student 0 for subjects 0: 90
Enter marks of student 0 for subjects 1: 89
Enter marks of student 0 for subjects 2: 78
Enter marks of student 0 for subjects 0: 67
Enter marks of student 0 for subjects 1: 88
Enter marks of student 0 for subjects 2: 99
Total marks obtained by student pritesh are 257
Total marks obtained by student suraj are 254
Student pritesh got maximum marks = 90 in subject :0
Student pritesh got maximum marks = 89 in subject :1
Student pritesh got maximum marks = 99 in subject :2
Pritesh obtained the total highest marks

Calculate Size of Structure in C Programming

#include
#include
struct stud
{
int roll;
char name[10];
int marks;
};
int main()
{
int size;
struct stud s;
size=sizeof(s);
printf("\n Size of structure :%d",size);
return(0);
}

 Length of the String using Pointer


#include
#include
int string_ln(char*);
void main()
{
char str[20];
int length;
clrscr();
printf("\n Enter any string :”);
gets(str);
lengh=string_ln(str);
printf("The length of the given string %s is : %d",str,length);
getch();
}
int string_n(char*p)
{
int count=0;
while(*p!=’\p’)
{
count++;
p++;
}
return count;
}

Output
          Enter the string : Pritesh
Length of the given string pritesh is :7

No comments:

Post a Comment