You are on page 1of 45

Arrays

Strings

Arrays and Strings


CS 11 - Introduction to Computer Programming I

Jan Michael C. Yap


janmichaelyap@gmail.com
Department of Computer Science
University of the Philippines Diliman

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

1 Arrays

2 Strings

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Topics

1 Arrays

2 Strings

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user


Declare three variables of type int, say input 1, input 2, and
input 3

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user


Declare three variables of type int, say input 1, input 2, and
input 3
Get one thousand(1,000) integers from the user

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user


Declare three variables of type int, say input 1, input 2, and
input 3
Get one thousand(1,000) integers from the user
WHAT THE FUCK FOR?!!!

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user


Declare three variables of type int, say input 1, input 2, and
input 3
Get one thousand(1,000) integers from the user
WHAT THE FUCK FOR?!!!
Do I really want to declare a thousand variables?!!!

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Motivation

Get three (3) integers from the user


Declare three variables of type int, say input 1, input 2, and
input 3
Get one thousand(1,000) integers from the user
WHAT THE FUCK FOR?!!!
Do I really want to declare a thousand variables?!!!
This is where the concept of arrays come in.

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Arrays

An array is a variable that hold a number of similarly-typed


values

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Arrays

An array is a variable that hold a number of similarly-typed


values
In C, the syntax for declaring arrays is:
<data type><array variable name>[<array size>];

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Arrays

An array is a variable that hold a number of similarly-typed


values
In C, the syntax for declaring arrays is:
<data type><array variable name>[<array size>];
int integer input[1000];
char character input[100];

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Arrays

An array is a variable that hold a number of similarly-typed


values
In C, the syntax for declaring arrays is:
<data type><array variable name>[<array size>];
int integer input[1000];
char character input[100];
You may also initially populate the arrays being declared:
<data type><array variable name>[<optional array size>] =
{ <comma separated list of values>};

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Arrays

An array is a variable that hold a number of similarly-typed


values
In C, the syntax for declaring arrays is:
<data type><array variable name>[<array size>];
int integer input[1000];
char character input[100];
You may also initially populate the arrays being declared:
<data type><array variable name>[<optional array size>] =
{ <comma separated list of values>};
int integer input[3] = {1,2,3};
char character input[] = {’a’, ’b’, ’c’, ’d’, ’X’};

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Accessing Array Elements

To access an element of an array, you need to place the index


of the element in between the square brackets.

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Accessing Array Elements

To access an element of an array, you need to place the index


of the element in between the square brackets.
integer input[2];
character input[1];

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Accessing Array Elements

To access an element of an array, you need to place the index


of the element in between the square brackets.
integer input[2];
character input[1];
A “quirk” in C: assuming an array of n elements, the index of
the first element is 0 while the index of the last element is n-1
When an array is declared, a sequence of memory blocks is
allocated, all of which have an address

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Accessing Array Elements

To access an element of an array, you need to place the index


of the element in between the square brackets.
integer input[2];
character input[1];
A “quirk” in C: assuming an array of n elements, the index of
the first element is 0 while the index of the last element is n-1
When an array is declared, a sequence of memory blocks is
allocated, all of which have an address
The start address where the first element is placed
The end address where the last element is placed.

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Accessing Array Elements

To access an element of an array, you need to place the index


of the element in between the square brackets.
integer input[2];
character input[1];
A “quirk” in C: assuming an array of n elements, the index of
the first element is 0 while the index of the last element is n-1
When an array is declared, a sequence of memory blocks is
allocated, all of which have an address
The start address where the first element is placed
The end address where the last element is placed.
The index is interpreted as the offset from the start address of
the memory space allocated to the array.

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Multidimensional Arrays

Arrays in C are not limited to a one-dimensional or linear


array, multidimensional arrays are possible

1
http://stackoverflow.com/questions/10738267/what-is-the-maximum-
number-of-dimensions-allowed-for-an-array-in-c
JMC Yap CS 11 - Arrays and Strings
Arrays
Strings

Multidimensional Arrays

Arrays in C are not limited to a one-dimensional or linear


array, multidimensional arrays are possible
To declare: <data type><array variable name>
[<size of dimension 1>] [<size of dimension 2>] ... ;

1
http://stackoverflow.com/questions/10738267/what-is-the-maximum-
number-of-dimensions-allowed-for-an-array-in-c
JMC Yap CS 11 - Arrays and Strings
Arrays
Strings

Multidimensional Arrays

Arrays in C are not limited to a one-dimensional or linear


array, multidimensional arrays are possible
To declare: <data type><array variable name>
[<size of dimension 1>] [<size of dimension 2>] ... ;
int multiplication table[10][10];

1
http://stackoverflow.com/questions/10738267/what-is-the-maximum-
number-of-dimensions-allowed-for-an-array-in-c
JMC Yap CS 11 - Arrays and Strings
Arrays
Strings

Multidimensional Arrays

Arrays in C are not limited to a one-dimensional or linear


array, multidimensional arrays are possible
To declare: <data type><array variable name>
[<size of dimension 1>] [<size of dimension 2>] ... ;
int multiplication table[10][10];
double data table[10][20][30];

1
http://stackoverflow.com/questions/10738267/what-is-the-maximum-
number-of-dimensions-allowed-for-an-array-in-c
JMC Yap CS 11 - Arrays and Strings
Arrays
Strings

Multidimensional Arrays

Arrays in C are not limited to a one-dimensional or linear


array, multidimensional arrays are possible
To declare: <data type><array variable name>
[<size of dimension 1>] [<size of dimension 2>] ... ;
int multiplication table[10][10];
double data table[10][20][30];
“The standard recommends... to accept at least 256
[dimensions] (ISO 14882, B.2), but they may support less or
more1 ”

1
http://stackoverflow.com/questions/10738267/what-is-the-maximum-
number-of-dimensions-allowed-for-an-array-in-c
JMC Yap CS 11 - Arrays and Strings
Arrays
Strings

Some Notes

Copying an array into another entails copying the contents of


the source array, one by one, into the destination array
Invalid: target array = source array;

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Some Notes

Copying an array into another entails copying the contents of


the source array, one by one, into the destination array
Invalid: target array = source array;
Manipulating an array entails manipulating the contents of
the source array, one by one
Invalid: int array = int array - 15;

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Topics

1 Arrays

2 Strings

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”
”I am a string!”

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”
”I am a string!”
”s0 aM I L0Lzzzz j3j3JejE”

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”
”I am a string!”
”s0 aM I L0Lzzzz j3j3JejE”
A string variable in C is an array of characters that has a
special escape character placed at the end: \0

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”
”I am a string!”
”s0 aM I L0Lzzzz j3j3JejE”
A string variable in C is an array of characters that has a
special escape character placed at the end: \0
char some string[33] = ”An example of a string variable.”;

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Strings

A string is a sequence of characters that is analogous to a


word in a language
A string value in C is a sequence of characters flanked by
double quotation marks
”Hello!”
”I am a string!”
”s0 aM I L0Lzzzz j3j3JejE”
A string variable in C is an array of characters that has a
special escape character placed at the end: \0
char some string[33] = ”An example of a string variable.”;
The \0 character is counted as an element in the string, so
allot an extra space for it when declaring a string variable

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

A library in C is specially crafted for string manipulation:


string.h

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

A library in C is specially crafted for string manipulation:


string.h
Consists of predefined functions for such purpose

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

A library in C is specially crafted for string manipulation:


string.h
Consists of predefined functions for such purpose
Need to add the preprogram directive #include <string.h>

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

Some useful functions


strlen(<string >): returns an integer representing the length
of the string (\0 excluded)

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

Some useful functions


strlen(<string >): returns an integer representing the length
of the string (\0 excluded)
strcmp(<string 1>, <string 2>): lexicographical comparison
of two strings
Gives a negative value if string 1 comes before string 2
Gives a positive value if string 2 comes before string 1
Gives zero if the two strings are equal

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

Some useful functions


strlen(<string >): returns an integer representing the length
of the string (\0 excluded)
strcmp(<string 1>, <string 2>): lexicographical comparison
of two strings
Gives a negative value if string 1 comes before string 2
Gives a positive value if string 2 comes before string 1
Gives zero if the two strings are equal
strcpy(<string variable name>, <string source>): copies the
value of string source to the specified string variable

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Manipulating strings in C

Some useful functions


strlen(<string >): returns an integer representing the length
of the string (\0 excluded)
strcmp(<string 1>, <string 2>): lexicographical comparison
of two strings
Gives a negative value if string 1 comes before string 2
Gives a positive value if string 2 comes before string 1
Gives zero if the two strings are equal
strcpy(<string variable name>, <string source>): copies the
value of string source to the specified string variable
strcat(<string variable name>, <string source>):
concatenates the value of string source to the specified string
variable

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Some Notes

Assigning a string constant to a character array intended to


be a string variable can only be via initialization of the array
Invalid: char string variable[30]; string variable = “Example”;

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

Some Notes

Assigning a string constant to a character array intended to


be a string variable can only be via initialization of the array
Invalid: char string variable[30]; string variable = “Example”;
When getting string input using the scanf function, white
spaces are treated as terminating symbols
This means, for example, that if input consists of two words
separated by a white space, only the first word would be stored
in the variable.
A function used to get around this is the gets function:
gets(<string variable name>)

JMC Yap CS 11 - Arrays and Strings


Arrays
Strings

END OF LESSON 4

JMC Yap CS 11 - Arrays and Strings

You might also like