39
Exercise 7 Strings

Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Exercise 7

Strings

Page 2: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Strings

An array of characters Used to store text Another way to initialize:

char A[ ]=“blabla”;

Page 3: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

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 7 chars we need an array of length 8

So the previous initialization is equivalent to

char A[ ] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};

Page 4: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

The fool on the null

#include <stdio.h>

int main(void){ char str[]="I'm a full string"; printf("%s\n", str); str[7]='o'; str[8]='o'; printf("%s\n", str); str[11]='\0'; printf("%s\n", str); str[11] = ‘s’; printf("%s\n", str);

return 0;}

Page 5: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

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()

Page 6: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Example

Read_String_with_getchar.c

Page 7: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

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 there’s no ‘&’ sign!!!

Page 8: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

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)

Page 9: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Example

Read_String_with_scanf.c

Page 10: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Comparing strings

We cannot just compare strings’ contents by == char A[7]=“Hello”; char B[7]=“Hello”;

if (A==B) {

... } Because A and B are addresses of A[0] and B[0]

A==B 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’

….

B

….

….

‘H’

‘e’

‘l’ ‘l’ ‘o’

‘\0’

….

A

….

….

Page 11: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Example

Compare.c

Page 12: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 13: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 14: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 15: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 16: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 17: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 18: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 19: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Compare – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘w’ ‘\0’

A

Page 20: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 21: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 22: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 23: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 24: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 25: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 26: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

3

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 27: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Equal strings – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

3

i

‘Y’ ‘e’ ‘s’ ‘\0’

A

Page 28: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘\0’

A

Page 29: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

0

i

‘Y’ ‘e’ ‘\0’

A

Page 30: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘\0’

A

Page 31: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘\0’

A

Page 32: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

2

i

‘Y’ ‘e’ ‘\0’

A

Page 33: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘\0’

A

Page 34: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Different length – step by step

for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])

{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;

B

‘Y’ ‘e’ ‘s’ ‘\0’

1

i

‘Y’ ‘e’ ‘\0’

A

Page 35: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

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…

Page 36: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Example

easy_compare.c

Page 37: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

If there’s 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”

Page 38: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Solution

replace.c

Page 39: Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

If there’s 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