Title: Exercise 7
1Exercise 7
2Multi-dimensional arrays
- Array of arrays
- int A23 1, 2, 3,
- 4, 5, 6
- Means an array of 2 integer arrays, each of
length 3. - Access j-th element of the i-th array is
- Aij
3Multi-dimensional arrays
- The size of the array can be determined by the
compiler - double B2 1,2, 2,3, 3,4
Cannot skip this!!
4Example matrix addition
include ltstdio.hgt define SIZE 3 int main()
double ASIZE 1,2,3, 4,5,6,
7,8,9 double BSIZE 1,1,1, 2,2,2,
3,3,3 double CSIZESIZE int i,
j for(i0 iltSIZE i) for(j0 jltSIZE
j) CijAij Bij return
0
5Strings
- An array of characters
- Used to store text
- Another way to initialize
- char A blabla
6The Terminator
- Strings terminate with NULL character, signed by
\0 (ascii code 0) - This is a convention used to know where the
string ends - It means that in order to hold a string of 6
chars we need an array of length 7 - So the previous initialization is equivalent to
- char A b, l, a, b, l, a,
\0
7The fool on the null
- include ltstdio.hgt
- int main(void)
-
- char str"I'm a full string"
- printf("s\n", str)
- str7'o'
- str8'o'
- printf("s\n", str)
- str11'\0'
- printf("s\n", str)
- str11 s
- printf("s\n", str)
- return 0
I'm a full string
I'm a fool string
I'm a fool
I'm a full string
8Reading-in strings
- There are several ways of accepting strings as
input from the user - The obvious way to go is read character by
character using getchar()
9Example
- include ltstdio.hgt
- define MAX_LENGTH 10
- int main(void)
- char sMAX_LENGTH1 / We need one more place
for the '\0' / - char c
- int i
-
- printf("Please enter a string\n")
- for (i0 iltMAX_LENGTH i)
- c getchar()
- if (c'\n')
- break
- si c
10Reading-in strings scanf
- A simpler way is to use scanf
- To read in a string to a variable str, write
- scanf(s, str)
- Note theres no sign!!!
11Reading-in strings - scanf
- scanf reads-in letters until a space or newline
(\n) is encountered - The maximum length can be stated in the
parentheses - scanf(10s, str)
- This will read in 10 letters, plus the \0 sign
(so str should have place for 11 characters)
12Reading-in strings - scanf
- scanf reads-in letters until a space or newline
(\n) is encountered - scanf(10s, str)
- This will read in 10 letters, plus the \0 sign
(so str should have place for 11 characters) - IMPORTANT better always to give maximum length
to scanf otherwise the program may be aborted
if the word entered is longer than our string
array.
13Example
- include ltstdio.hgt
- define MAX_LENGTH 5
- int main(void)
- char strMAX_LENGTH1 / We need one more
place for the '\0' / -
- printf("Please enter a string\n")
- scanf("5s", str) / Note - no '' sign!!! /
- printf("The string you entered is - s\n", str)
- return 0
-
14Example
- include ltstdio.hgt
- define MAX_LENGTH 5
- int main(void)
- char strMAX_LENGTH1
- char c
- scanf("5s", str)
- scanf("c", c)
- printf("str is - s\n", str)
- printf(c - c\n", c)
- return 0
-
USER INPUT 12345\n
str is - 12345 c \n
15Example
- include ltstdio.hgt
- define MAX_LENGTH 5
- int main(void)
- char strMAX_LENGTH1
- char c
- scanf("5s", str)
- scanf(\nc", c)
- printf("str is - s\n", str)
- printf(c - c\n", c)
- return 0
-
USER INPUT 12345\n x
str is - 12345 c x
16Comparing strings
- We cannot just compare strings contents by
- char A7Hello
- char B7Hello
- if (AB)
-
- ...
-
- Because A and B are addresses of A0 and B0
- AB only if A and B are the same string in
memory - In order to compare the contents we must scan
char by char
H
e
l
l
o
\0
.
.
.
H
e
l
l
o
\0
.
.
.
B
A
17Example
- include ltstdio.hgt
- int main(void)
- int i
- char A101
- char B101
- printf("Enter first string\n")
- scanf("100s",A)
- printf("Enter second string\n")
- scanf("100s",B)
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
- printf("A is different from B!\n")
- return 0
-
18Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
0
19Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
0
20Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
1
21Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
1
22Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
2
23Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
2
24Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
2
25Compare step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
w
\0
i
2
26Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
0
27Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
0
28Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
1
29Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
1
30Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
2
31Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
2
32Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
3
33Equal strings step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
s
\0
i
3
34Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
0
35Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
0
36Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
1
37Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
1
38Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
2
39Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
1
40Different length step by step
A
Y
e
s
\0
- for(i0 Ai!'\0' Bi!'\0' i)
- if(Ai!Bi)
-
- printf("A is different from B!\n")
- return 0
-
-
- printf("A and B are the same!\n")
- return 0
B
Y
e
\0
i
1
41String library
- Like in the case of stdio.h and math.h, we have a
special library for handling strings - We should include string.h
- Functions
- strlen(s) returns the length of s
- strcmp(s1, s2) compares s1 with s2
- strcpy(s1, s2) copies to contents of s2 to s1
- and more
42Example
easy_compare.c
43If theres time
- Exercise write a program that
- gets an input string from the user (up to 100
chars, no white spaces) - gets 2 characters from the user
- replaces each occurrence of the first character
in the string by the second character, and prints
the result. - Example
- input papa, p, m
- output mama
44Solution
replace.c
45If theres time
- Exercise write a function
- void my_to_lower(char str)
- The function receives an arbitrary string and
converts all its letters to lower-case letters - Demonstrate the use of your function by some
example that prints the input and output on the
screen