1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.

Slides:



Advertisements
Similar presentations
Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Chapter 10.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Programming Review: Functions, pointers and strings.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 More on Pointers. 2 Reminder 3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7.
Exercise 10 Review: pointers, strings and recursion.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Strings in C.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
CHAPTER 8 CHARACTER AND STRINGS
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Strings, Pointers and Tools
Computer Programming for Engineers
Strings, Slide Fundamental Programming Strings.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Lecture 8 String 1. Concept of strings String and pointers
Strings (מחרוזות).
CSE 1320 Search, Sort and Strings
A First Book of ANSI C Fourth Edition
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
C Programming Strings.
Lecture 11 Strings.
EECE.2160 ECE Application Programming
Chapter 8 Character Arrays and Strings
String manipulation string.h library
EECE.2160 ECE Application Programming
Exercise Arrays.
Strings in C Array of characters is called a string.
Programming Strings.
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Presentation transcript:

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