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

Array Strings in Programiing

Module 3 covers arrays and strings in C programming, detailing their declaration, initialization, and manipulation. It explains one-dimensional and two-dimensional arrays, including examples of their usage and initialization techniques. The module also introduces strings as character arrays, their declaration, initialization, and common string handling functions.

Uploaded by

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

Array Strings in Programiing

Module 3 covers arrays and strings in C programming, detailing their declaration, initialization, and manipulation. It explains one-dimensional and two-dimensional arrays, including examples of their usage and initialization techniques. The module also introduces strings as character arrays, their declaration, initialization, and common string handling functions.

Uploaded by

salunkeareen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Module 3

Arrays and Strings


By
Ms. Harshal P G
Asst. Professor, SFIT
MODULE 3

Arrays and Strings:

3.1 Arrays: Declaration, Initialization, Accessing elements, Array manipulation,


Multi-dimensional arrays

3.2 Strings: Declaration, Initialization, Accessing elements, String manipulation,


String library functions
Introduction to Arrays
• So far we have used only the fundamental data types, namely char, int, float,
double and variations of int and double.
• Although these types are very useful, they are constrained by the fact that a
variable of these types can store only one value at any given time.
• Therefore, they can be used only to handle limited amounts of data. In many
applications, however, we need to handle a large volume of data in terms of
reading, processing and printing.
• To process such large amounts of data, we need a powerful data type that would
facilitate efficient storing, accessing and manipulating of data items.
• C supports a derived data type known as array that can be used for such
applications.
Introduction to Arrays (Contd…)
• An array is a Fixed Sized sequenced collection of elements of the same data type.
• It is simply a grouping of like-type data. In its simplest form, an array can be used
to represent a list of numbers, or a list of names.
• Some examples where the concept of an array can be used:
✔ List of temperatures recorded every hour in a day, or a month, or a year.
✔ List of employees in an organization.
✔ List of products and their cost sold by a store.
✔ Test scores of a class of students.
✔ List of customers and their telephone numbers.
✔ Table of daily rainfall data.
Introduction to Arrays (Contd…)
• We can use arrays to represent not only simple lists of values but also tables of
data in two, three or more dimensions.
• One-dimensional arrays
• Two-dimensional arrays
• Multidimensional arrays

Dr. Tatwadarshi P. N.
One Dimensional Array (Contd…)
• A list of items can be given one variable name using only one subscript and such a
variable is called a Single-Subscripted variable or a One-Dimensional array.
• In mathematics, we often deal with variables that are single-subscripted. For
instance, we use the equation.

• to calculate the average of n values of x. The subscripted variable xi refers to the


ith element of x.
• In C, single-subscripted variable xi can be expressed as
• x[1], x[2], x[3],.........x[n]
One Dimensional Array (Contd…)
Declaration of One Dimensional Array
• Like any other variable, arrays must be declared before they are used so that the
compiler can allocate space for them in memory.
• The general form of array declaration is

type variable-name[ size ]

• Ex:
✔ float height[50];
✔ int group[10];
✔ char name[10];

Dr. Tatwadarshi P. N.
Initialisation of One Dimensional Array
• After an array is declared it has to be initialised. Otherwise, they will contain garbage
values.

• An array can be initialised in two ways:


✔ At compile time
✔ At run time
Compile time Initialisation of Array
• We can initialize the elements of arrays in the same way as the ordinary variables
when they are declared.
• The general form of initialization of arrays is:
type array-name[size] = { list of values };

• The values in the list are separated by commas. For example, the statement
int number[3] = { 0,0,0 };
Compile time Initialisation of Array
• float total[5] = {0.0,15.75,–10};
First three elements are initialised remaining 2 will be zero.

• The size may be omitted. In such cases, the compiler allocates enough space for all
initialized elements.
• For example, the statement
int counter[ ] = {1,1,1,1};
Run time Initialisation of Array
• An array can be explicitly initialized at run time.
• This approach is usually applied for initializing large arrays

for (i = 0; i < 100; i = i+1)


{
if i < 50
arr[i] = 0.0; /* assignment statement */
else
arr[i] = 1.0;
}
Two Dimensional Array
• So far we have discussed the array variables that can store a list of values.
• There could be situations where a table of values will have to be stored. Consider the
following data table, which shows the value of sales of three items by four sales girls:

• We can think of the table as a matrix consisting of four rows and three columns.
Declaration Two Dimensional Array
• Two-dimensional arrays are declared as follows:

• type array_name [row_size] [column_size];

• For the table discussed above, the declaration will be


v[4][3]
Declaration Two Dimensional Array
Initialization of Two Dimensional Array
• Like the one-dimensional arrays, two-dimensional arrays may be initialized by
following their declaration with a list of initial values enclosed in braces.
• For example,
int table[2][3] = { 0,0,0,1,1,1};
Note that the Initialisation is done row by row
So, for the above example fist row will have 0’s and the second row will have 1’s.
• The above statement can be equivalently written as
int table[2][3] = {{0,0,0}, {1,1,1}};
by surrounding the elements of the each row by braces.
Initialization of Two Dimensional Array (Contd…)
• We can also initialize a two-dimensional array in the form of a matrix as shown
below:
int table[2][3] = {
{0,0,0},
{1,1,1}
};
• When the array is completely initialized with all values, explicitly, we need not
specify the size of the first dimension
int table [ ] [3] = {
{ 0, 0, 0},
{ 1, 1, 1}
};
Initialization of Two Dimensional Array
(Contd…)
• If the values are missing in an initializer, they are automatically set to zero. For instance,
the statement
• int table[2][3] = {
• {1,1},
• {2}
• };

• When all the elements are to be initialized to zero, the following short-cut method may
be used.
• int m[3][5] = { {0}, {0}, {0}};
Strings
Strings
• A string is a sequence of characters that is treated as a single data item.
• Any group of characters (except double quote sign) defined between double quotation
marks is a string constant.
• Example:
“Man is obviously made to think.”
• Character strings are often used to build meaningful and readable programs. The
common operations performed on character strings include:
reading and writing
Combining strings together.
Copying one string to another.
Comparing strings for equality.
Extracting a portion of a string.
Declaring and Initialising String variables
• In C the string are represented as character arrays.
• So, a string variable is any valid C variable name and is always declared as an array of
characters.

• The general form of declaration of a string variable is:


char string_name[ size ];

• The size determines the number of characters in the string_name.Some examples are:
char city[10];
char name[30];
Declaring and Initialising String variables
(Contd…)

• When compiler assigns a character string to a character array, it automatically supplies


NULL character (‘/0’) at the end of the string.
• Therefore the size of the character array should be equal to the maximum number of
characters to be initialised in the string PLUS one.

• Like numeric arrays, character arrays can be initialised when they are declared.
• C permits a character array to be initialised in either of the following two forms:
char city [9] = “ NEW YORK ”;
char city [9]={‘N’,‘E’,‘W’,‘ ‘,‘Y’,‘O’,‘R’,‘K’,‘\0’};
Declaring and Initialising String variables
(Contd…)
• We can also initialize the character array without specifying the size of the array.

char string [ ] = {‘G’,‘O’,‘O’,‘D’,‘\0’};


Taking String from the user
• We cannot use the familiar input function scanf %s because it terminates when it
encounters a space

• Example:
char address[10]
scanf(“%s”, address);
NEW YORK
Taking String from the user (Contd…)
• We can read a single character from the terminal using the function getchar.
• We can use this function repeatedly to read successive single characters from the
input and place them into an array.
• char ch;
• ch = getchar( );

• The drawback is we’ll have to use looping for taking the string
Taking String from the user (Contd…)
• The more convinent way is using the library function gets available in the <stdio.h>
header file
• It is a simple function with one string parameter and called as under:
gets (str);
• str is a string variable declared properly.
• It reads characters into str from the keyboard until a new line character is encountered
and then appends a null character to the string.

• char line [80];


• gets (line);
• printf (“%s”, line);
String handling functions
• Following are the most commonly used string handling functions
String handling functions (Contd…)
• The strcat function joins the two string together
strcat(string1, string2);
• The strcmp function compares the two strings and has a value 0 if both the strings
are equal
strcmp(string1, string2);
• string1 and string2 may be string variables or string constants.
• Examples are:
strcmp(name1, name2);
strcmp(name1, “John”);
strcmp(“Rom”, “Ram”);
String handling functions (Contd…)
• The strcpy is similar to an assignment operator
strcpy(string1, string2);
• Assigns the contents of string2 to string1.
• string2 may be a character array variable or a string
strcpy(city, “DELHI”);

• The strln function counts and returns the number of characters in a string. It takes
the form

n = strlen(string);

• Where n is an integer variable , which receives the value of the length of the
string.

You might also like