Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Exercise 7 Strings

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

3 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’};

4 The fool on the null #include 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; }

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

6 Example Read_String_with_getchar.c

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

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

9 Example Read_String_with_scanf.c

10 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’ ‘o’‘\0’…. B ‘H’‘e’‘l’ ‘o’‘\0’…. A

11 Example Compare.c

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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…

36 Example easy_compare.c

37 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”

38 Solution replace.c

39 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


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

Similar presentations


Ads by Google