0% found this document useful (0 votes)
47 views

2D Arrays and Structures

The document discusses limitations of scanf() and puts() functions in C and how to overcome them. It introduces gets() and puts() to receive and print multi-word strings. It also discusses standard string library functions, two-dimensional arrays, structures, and arrays of structures to store student data like name, age, and marks.

Uploaded by

Dinesh Lasantha
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

2D Arrays and Structures

The document discusses limitations of scanf() and puts() functions in C and how to overcome them. It introduces gets() and puts() to receive and print multi-word strings. It also discusses standard string library functions, two-dimensional arrays, structures, and arrays of structures to store student data like name, age, and marks.

Uploaded by

Dinesh Lasantha
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

The limitation of using %s in scanf

scanf() is not capable of receiving multi-word strings such as “Dennis


Ritchie” separated by a space

Use gets() and puts() to overcome that


Use of gets() and puts()
main()
{ char name[25];
printf(“Enter your name please “);
gets(name);
puts(“Hello!”);
puts(name);
}

The output will be


Enter your name please Dennis Ritchie
Hello!
Dennis Ritchie
Limitation of puts()
WHY puts(“Hello”) and puts(name);

puts() can print only one string at a time, and it places the cursor on next
line

Similarly gets() can receive only one string at a time although it can be a
multi-word string
Standard Library – String
functions
Function Use
strlen Finds length of a string
strlwr Converts a string to lowercase
strupr Converts a string to uppercase
strcat Appends one string at the end of another
strncat Appends first n characters of a string into
another
strncmp Compares two strings
strcmpi Compares two strings without regard to
stricmp case
strnicmp Compares first n characters of two strings
without regard to case
Standard Library – String functions
cntd…
strdup Duplicates a string
strchr Finds first occurrence of a given
character in a string
strrchr Finds last occurrence of a given
character in a string
strstr Finds first occurrence of a given string in
another string
strset Sets all characters of a string to a given
character
strnset Sets first n characters of a string to a
given character
strrev Reverses string
Two dimensional arrays
You can have a two dimensional array to store
student number and the marks
int stud[10][2];
int i,j;
for (i=0;i<=9;i++)
{ printf(“ Enter student number and marks”);
scanf(“%d %d”,&stud[i][1],&stud[i][2]);
}

Same way one can print the elements of the 2-d array
Initialization of a 2-d array
can be done in the following
ways
int stud[5][2] = { {1000, 56}, {1001,45},
{1002,78}, {1003,84},
{1004,63}};

int stud[5][2] = { 1000,56,1001,45,1002,78, 1003,84,1004,63 };


Structures
int marks[30];
to hold student marks only

int stud[30][2];
to hold student number and marks

Whether 1-D or 2-D…..an array should be of the same type


What if we want to store the
name and the marks?

A single array cannot handle it

Structures will help you to do so


Student details in three arrays
char names[];
int ages[3];
int marks[3];

printf(“Enter name, age and marks of the student”);

for (i=0;i<=2;i++)
scanf(“%c %d %d”,&name[i],&age[i],&mark[i]

for (i=0;i<=2;i++)
printf(“%c %d %d\n”,name[i],age[i],mark[i]
Use of structure…..
typedef struct student
{ char name;
int age;
int mark;
};
student s1,s2,s3;
printf(“Enter name, age and marks of the students”);

scanf(“%c %d %d”,&s1.name,&s1.age,&s1.mark);
scanf(“%c %d %d”,&s2.name,&s2.age,&s2.mark);
scanf(“%c %d %d”,&s3.name,&s3.age,&s3.mark);

printf(“\n%c %d %d”,s1.name,s1.age,s1.mark);
printf(“\n%c %d %d”,s2.name,s2.age,s2.mark);
printf(“\n%c %d %d”,s3.name,s3.age,s3.mark);
The above will also be complex
when there are many students,
say 100
Array of structures is the solution

struct student
{ char name;
int age;
int mark;
};
struct student s[100];
int i;
printf(“Enter name, age and marks of the 100 students”);

for (i=0;i<=99;i++)
scanf(“%c %d %d”,&s[i].name,&s[i].age,&s[i].mark);

for (i=0;i<=99;i++)
printf(“\n%c %d %d”,s[i].name,s[i].age,s[i].mark);

You might also like