Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write a program to check whether the given string is palindrome.

Object II. To modify the program # lab report 11.2 such that it uses pointer to structure instead of structure itself.

Download this full program | Lab 12 | in pdf format

Background Theory:

Structures and Functions: Of course a sensible alternative to writing out the addition each time is to define a function to do the same job - but this raises the question of passing structures as parameters. Fortunately this isn't a big problem. Most C compilers, will allow You to pass entire structures as parameters and return entire structures. As with all C parameters structures are passed by value and so if you want to allow a function to alter a parameter you have to remember to pass a pointer to a struct.

 

Pointers to Structures: You can define a pointer to a structure in the same way as any pointer to any type. For example: struct emprec *ptr defines a pointer to an emprec. You can use a pointer to a struct in more or less the same way as any pointer but the use of qualified names makes it look slightly different For example: (*ptr).age is the age component of the emprec structure that ptr points at - i.e. an int. You need the brackets because '.' has a higher priority than '*'. The use of a pointer to a struct is so common, and the pointer notation so ugly, that there is an equivalent and more elegant way of writing the same thing. You can use:

prt->age to mean the same thing as (*ptr).age. The notation gives a clearer idea of what is going on - prt points (i.e. ->) to the structure and .age picks out which component of the structure we want. Interestingly until C++ became popular the - > notation was relatively rare and given that many C text books hardly mentioned it this confused many experienced C programmers!

 

Palindrome:  A palindrome is a word, phrase or sentence that reads the way either forward or backward. Eg: noon, madam. String length is determined and the characters are tested for equality from the first and the last.

 

Code:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

            char str[20];

            int right, left, val,match;

            clrscr();

 

            printf("\nEnter a string\n");

            scanf("%s",str);

            val=strlen(str);

            right=val-1;

            left=0;

 

            while(left!=right)

            {

                        if (str[left]==str[right])

                        {

                                    match=1;

                                    left++;

                                    right--;

                        }

 

                        else

                        {

                        match=0;

                        break;

                        }

              }

               if(match==1)

               printf("\nThe given string > %s < is a palindrome", str);

               else

               printf("\nThe given string > %s < is not a palindrome", str);

            getch();

}

#include<stdio.h>

#include<conio.h>

void main()

{

int nums, i;

struct student

{

char name[20],remark[20];

int roll, marks;

};

struct student st[10],*ptr;

 

clrscr();

ptr=st;

printf("How much number of students do you want?\t");

scanf("%d",&nums);

for(i=1;i<=nums;i++)

{

printf("\nEnter name, remark, roll and marks of student %d:\t",i);

scanf("%s%s%d%d",ptr->name,ptr->remark,&ptr->roll,&ptr->marks);

ptr++;

}

Printf(\n\nYour Entry:\n);

ptr=st;

for(i=1;i<=nums;i++)

{

printf("\n%s\t%s\t%d\t%d\t",ptr->name,ptr->remark,ptr->roll,ptr->marks);

ptr++;

}

getch();

}

 

To Download this full program | Lab 12 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com