Exercise 7 - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Exercise 7

Description:

... only if A and B are the same string ... 'A and B are the same!n'); return 0; Compare step ... printf('A and B are the same!n'); return 0; B. Y' e' s' ' 0 ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 46
Provided by: csta3
Category:
Tags: define | exercise

less

Transcript and Presenter's Notes

Title: Exercise 7


1
Exercise 7
  • Strings

2
Multi-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

3
Multi-dimensional arrays
  • The size of the array can be determined by the
    compiler
  • double B2 1,2, 2,3, 3,4

Cannot skip this!!
4
Example 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
5
Strings
  • An array of characters
  • Used to store text
  • Another way to initialize
  • char A blabla

6
The 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

7
The 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
8
Reading-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()

9
Example
  • 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

10
Reading-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!!!

11
Reading-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)

12
Reading-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.

13
Example
  • 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

14
Example
  • 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
15
Example
  • 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
16
Comparing 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
17
Example
  • 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

18
Compare 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
19
Compare 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
20
Compare 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
21
Compare 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
22
Compare 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
23
Compare 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
24
Compare 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
25
Compare 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
26
Equal 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
27
Equal 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
28
Equal 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
29
Equal 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
30
Equal 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
31
Equal 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
32
Equal 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
33
Equal 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
34
Different 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
35
Different 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
36
Different 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
37
Different 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
38
Different 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
39
Different 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
40
Different 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
41
String 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

42
Example
easy_compare.c
43
If 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

44
Solution
replace.c
45
If 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
Write a Comment
User Comments (0)
About PowerShow.com