0% found this document useful (0 votes)
15 views26 pages

Day 4

The document discusses strings and arrays in C. It defines arrays as variables that contain a set of data items of the same type stored in contiguous memory locations. Arrays can be one-dimensional, two-dimensional, or multi-dimensional. Strings are arrays of characters terminated by a null character. Common string functions include strcpy(), strcat(), strcmp(), and strlen(). Arrays and strings are fundamental data types in C that allow storing and manipulating lists of values.

Uploaded by

khitcsec3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

Day 4

The document discusses strings and arrays in C. It defines arrays as variables that contain a set of data items of the same type stored in contiguous memory locations. Arrays can be one-dimensional, two-dimensional, or multi-dimensional. Strings are arrays of characters terminated by a null character. Common string functions include strcpy(), strcat(), strcmp(), and strlen(). Arrays and strings are fundamental data types in C that allow storing and manipulating lists of values.

Uploaded by

khitcsec3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

DAY – 4

BASICS OF C
What will you learn?
 Arrays
 Types of Arrays
 Strings
 String Handling Functions
ARRAYS
ARRAY
 An array is a user defined variable that contains a set of data items of same
type that share a common name those are stored in continuous memory
locations.

 The individual values in an array are called elements.

 Each element is referenced by a number called index number (or) subscript


in square brackets after the array name.

Arrays are used to represent list of values in DIFFERENT dimensions. Hence


arrays are classified into three categories

1. One Dimensional Array

2. Two Dimensional Array

3. Multi Dimensional Arrays


ONE – DIMENSIONAL ARRAY
 If elements in an array are referenced/accessed with only one

index/subscript, then such an array is called One – dimensional array. A

one – dimensional array is declared as follows:

Datatype array-name [size];


• Datatype specifies what kind of values it can store – char, int, float,

long, etc.
• Array-name is name of the array used to identify

• Size is the number of values that the array can hold. Once you define

the size of an array it cannot be changed further.


EXAMPLE:

int marks [10];

The above statement declares an array variable named “marks” that

can hold 10 integers. Each of these elements is accessed with the use

of index.
MEMORY REPRESENTATION OF AN ARRAY:

Marks

Index 0 1 2 3 4 5 6 7 8 9

 The value of index starts from 0 and ends with (n – 1), where n is size
of an array. In the above example, index ranges from 0 to 9. This
means that the index of first element of an array is 0, second element
index is 1, third element index is 2, etc.
 For example, the third element of “marks” array is accessed as
marks[2]. Similarly, sixth element is accessed as marks[5].
INITIALIZATION OF AN ARRAY
Declaring of an array means that it only allocated the memory
to store the values to the specified size of an array. No values are
stored in the array. So storing elements into an array at the time of its
declaration is called initialization. An array is initialized as follows:

datatype array_name[size] = {list of values};


EXAMPLE:
int marks[5] = { 90,87,75,81,86};

The above statement creates an array that has allocated with


memory space to store 5 values as follows:

marks[0] 90
marks[1] 87
marks[2] 75
marks[3] 81
marks[4] 86
ASSIGNMENT OF VALUES
Here, elements of an array are given values individually using the
assignment operator. This can be done in the following way:

Array – name [index] = value;


Example:
marks[0]=10;
marks[1]=20;
marks[2]=30;
marks[3]=40;
INPUTTING VALUES
An array can be filled by inputting values from the keyboard. In this
case, a loop structure is used to input the value for each element of
an array.

The following code sample is used:

int i, marks[10];

for(i=0;i<10;i++)

scanf(“%d”, &marks[i]);
DISPLAYING OF ELEMENTS:
 As in the case of inputting values into an array, for displaying the
array elements also a loop structure is used.

 The code should be like as follows:

int i, marks[10];

for(i=0;i<10;i++)

printf(“%d\t”, marks[i]);
TWO – DIMENSIONAL ARRAYS
An array is said to be two – dimensional array when it is used
the two subscripts to access the elements in the array - one index for
the row and the other index for the column. Two – Dimensional arrays
are used to store the data in the form of matrices.

A two – dimensional array is represented as follows:

Column Index  0 1 2 3
Row Index  0
1
2
DECLARATION OF 2D ARRAY
A two – dimensional array is declared as follows:

data_type array – name [row size] [column size];

For example, to store the marks of 3 students in 5 subjects, then we declare the array as:

int marks[3][5];

The above statement creates an array with the size of 3 rows and 5 columns as shown
below:

Column 0 Column 1 Column 2 Column 3 Column 4

Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4]

Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4]

Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4]


INITIALIZATION OF 2D ARRAYS
At the time of declaration of a 2D array if we give the values
to store in it then it is called initialization of array. A 2D array is
initialized in the following ways:

For example,

int marks [2][3] = { 80,87,91,92,70,85};

A (2 x 3) array can hold 6 elements so 6 elements are given all at a


time. A 2D array can also be initialized row by row. This can be done as
follows:

int marks[2][3] = { {80,87,91},{92,70,85} };


INPUTTING ELEMENTS TO 2D ARRAY
In order to input the values from the keyboard you must use the
following code:
int marks[2][3]; 0 1 2

for ( i = 0 ; i < 2 ; i++) 0 10 11 12

{ 1 14 15 16
for ( j = 0 ; j < 3 ; j++)
{
i=2
scanf(“%d”, &marks [i][j]);
} j =3
}
DISPLAYING ELEMENTS OF 2D ARRAY
As in the case of inputting values into an array, for displaying the array elements
also a nested loop structure is used. The code should be like as follows:
int marks[2][3];
for ( i = 0 ; i < 2 ; i++)
{
for ( j = 0 ; j < 3 ; j++)
{
printf(“%d ”,marks[i][j]);
}
}
STRINGS
STRING HANDLING
STRING
“A string is defined as sequence of characters that is treated as
single data item”.

“A string is variable length array of characters that is delimited by


the null character”.

Examples “abcd”

“Ab123cde”

“Ramu”

Char name[ 20];


DECLARATION OF STRINGS /
CHARACTER ARRAY DECLARATION
 The C language does not support string data type.

 As a string is declared as array of characters hence the general form of string

declaration is as follows:

char string_name[size];
 In the above syntax the size specifies the number of characters in the string_name.

 For example

char city[10];
 We can declare the string as a pointer as follows:

char *ptr;
 Here the ptr is a pointer variable and the memory is allocated dynamically for it only

not for the string.


STRING IN C – MEMORY STORAGE
 The string is always ended with a null character ‘\0’.

 The characters after the null character are ignored.

 e.g., char str[20] = “Initial value”;

[0] [13]

I n i t i a l v a l u e \0 ? ? …
STRING IN C – INPUT/OUTPUT
 The placeholder %s is used to represent string arguments in printf and scanf.
 Example:
char message1[12] = "Hello world";
printf(“%s”,message1);

H e l l o w o r l d \0

 Example:
char message2[12];
scanf(“%s”,message2);

H e l l o \0 ? ? ? ? ? ?
STRING IN C – LIBRARY FUNCTIONS

Function Purpose Example


strcpy Makes a copy of a string strcpy(s1, “Hi”);

strcat strcat(s1, “more”);


Appends a string to the end
of another string
strcmp Compare two strings strcmp(s1, “Hu”);
alphabetically

strlen Returns the number of strlen(“Hi”) returns 2.


characters in a string
Breaks a string into tokens
strtok strtok(“Hi, Chao”, “ ,”);
by delimiters.
STRING IN C – LIBRARY FUNCTIONS
Function Purpose Example
Strncpy Copy the specified number of strncpy(s1, “SVN”,2);
characters

Strncmp Compare two string up to strncmp(“mo”, “more”, 2);


given n character

Stricmp Compare two strings


alphabetically without case stricmp(“hu”, “Hu”);
sensitivity.

strlwr Converts string to all strlwr(“Hi”) returns hi.


lowercase

strupr Converts s to all strupr(“Hi”);


uppercase
STRING IN C – LIBRARY FUNCTIONS

Function Purpose Example


Strncat Appends a string to the end of strncat(s1, “more”,2);
another string up to n characters

Reverses all characters in s1


Strrev strrev("more”);
(except for the terminating null)

You might also like