1 Strings ( מחרוזות )
2 Agenda Definition and initialization Termination Input / Output String library
3 Strings A sequence of characters with a meaning Stored in an array of chars Libraries/functions designed to work on strings
4 Strings Better initialize: char str[] = "Text"; then: char str[] = {‘T’,’e’,’x’,’t’};
5 Where does it end? 's''#'' 'f''d''y''4''7''$''_''e''g''d''.''p''v'…. 'H''e''l' 'o'' 'w''o''r''l''d''g''d''.''p''v'…. 'H''e''l' 'o'' 'w''o''r''l''d' '.''p''v'…. '\0' Terminator str This means we will be able to perform operations on string without knowing its length in advance
6 The Terminator Strings terminate with NULL character, signed by ‘\0’ (ascii code 0) A convention Thus, in order to hold a string of N chars we need an array of length at least N+1 Example: previous initialization is equivalent to char A[] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};
7 Printing strings printf allows printing whole strings at once using %s – char str[200]; printf(“%s\n”, str); This will print the contents of str until a ‘\0’ is encountered
8 Example 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[10] = '\0'; printf("%s\n", str); str[10] = 's'; printf("%s\n", str); return 0; } I’m a full string I’m a fool string I’m a fool I’m a foolsstring …
9 Reading strings: getchar() #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; /* We need one more place for the '\0' */ char c; int i; printf("Please enter a string:\n"); i = 0; c = getchar(); while (c >= 0 && c != '\n' && i < MAX_LENGTH) { str[i] = c; ++i; c = getchar(); } str[i] = '\0'; /* Terminate the string */ printf("The string you entered is: %s\n", str); return 0; }
10 Reading strings: scanf Another way is to use scanf To read in a string to a variable str, write : scanf("%s", str); Note there is no ‘&’ sign! Why?
11 Reading 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 10 letters, plus the '\0' sign (so str should be of size 11)
12 Example – using scanf #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; printf("Please enter a string:\n"); scanf("%20s", str); printf("The string you entered is: %s\n", str); return 0; }
13 scanf problem After using scanf the next character that will be read is the space or newline. For example: scanf("%s", str); scanf("%c", &tav); Here tav has the value ‘ ’ or newline (‘\n’).
14 Solving the problem We need to read and discard the unwanted newline. Either use getchar or inform scanf to expect spaces (also newline) before the next character. scanf("%s", str); scanf(" %c", &tav);
15 Comparing strings We cannot just compare strings’ contents with == char A[6]=“Hello”; char B[6]=“Hello”; if (A==B) Why? 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
16 Comparing Strings - Example int i; char A[101], B[101]; printf("Enter first string\n"); scanf("%100s",A); printf("Enter second string\n"); scanf("%100s",B); 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");
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’ 0 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’ 0 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’ 1 i ‘Y’‘e’‘w’‘\0’ A
20 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
21 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
22 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
23 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
24 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
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’ 0 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’ 0 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’ 1 i ‘Y’‘e’‘s’‘\0’ A
28 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
29 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
30 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
31 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
32 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
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’ 0 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’ 0 i ‘Y’‘e’‘\0’ A
35 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
36 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
37 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
38 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
39 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
40 Exercise implement the function replace void replace(char str[], char what, char with); The function scans the string and replaces every occurrence of the first char with the second one. write a program to test the above function the program should read a string from the user (no spaces) and two characters, then call the function with the input, and print the result. example input: “papa”, ‘p’, ‘m’ output: “mama”
41 Solution void replace(char str[], char replace_what, char replace_with) { int i; for (i = 0; str[i] != '\0'; ++i) { if (str[i] == replace_what) str[i] = replace_with; }
42 Solution #define STRING_LEN 100 int main(void) { char str[STRING_LEN + 1]; char replace_what, replace_with; printf("Please enter a string (no spaces)\n"); scanf("%100s", str); printf("Letter to replace: "); scanf(" %c", &replace_what); printf("Letter to replace with: "); scanf(" %c", &replace_with); replace(str, replace_what, replace_with); printf("The result: %s\n", str); return 0; }
43 String library Like in the case of stdio.h and math.h, we have a special library for handling strings We should #include
44 String library All functions assume that a string ends with ‘\0’. Useful functions: int strlen(const char s[]) returns the length of s int strcmp(const char s1[], const char s2[]) compares s1 with s2 Returns 0 if they are equal, -1 if s1 s2 strcpy(char s1[], const char s2[]) copies to contents of s2 to s1 and more…
45 ctype library #include Useful operation on single characters int isalpha(char c); int islower(char c); int isupper(char c); int isdigit(char c); char tolower(char c); char toupper(char c);
46 home Implement the following function: int evaluate(char expr[]); ‘evaluate’ calculate the value of a mathematical expression comprised of positive numbers and the operations ‘+’ and ‘-’ For example: Input: “ ” Output: 11 You may assume that the string is valid and doesn’t contain spaces. Also, that all numbers are single digit Test your function!
47 Guidance Read a number and according to the operation add or subtract from the current result. How do we transform a digit-char to an digit-integer? Answer: -’0’ How do we handle the first number? last one?
48 Solution int evaluate(char expr[]) { char op = '+'; int i, res = 0; i = 0; while (expr[i] != '\0') { if (op == '+') { res += char2int(expr[i]); } else { res -= char2int(expr[i]); } ++i; op = expr[i]; if (op != '\0') ++i; } return res; }
49 home Implement the function int my_strchr(char s[], char c); Should return the index of the first occurrence of c in s, -1 if there is none Write a program that accepts as input a string and two chars, c1 and c2, and replaces every occurrence of c1 with c2 Solution: my_strchr.c
50 home Write a program that gets a string from the user and checks whether or not it is a palindrome Example for a palindrome: abbcbba (Hint: use strlen…)
51 Palindrome.c /* This program checks whether a given string is a palindrome*/ #include int main(void) { int len,i; char str[101]; printf("Enter a string\n"); scanf("%100s",str); len = strlen(str); for (i=0; i<len/2; i++) if (str[i] != str[len-i-1]) { printf("The string is not a palindrome!\n"); return 0; } printf("The string is a palindrome!\n"); return 0; }
52 home Implement the string.h function strrchr that returns the index of the last occurrence of a character inside a string. Write a program that accepts a string and char from the user, displays the index of its last occurrence Solution: strrchr.c
53 home Implement the following function – Input – two strings str1, str2 Output – the index of the first instance in str1 of any of the characters contained in str2 Hint – use the replace function Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces strcspn.c