Strings (מחרוזות).

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

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'; }
For loops For loops are controlled by a counter variable. for( c =init_value;c
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Programming Review: Functions, pointers and strings.
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.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
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.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
EENG212 Algorithms and Data Structures
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
UNIT 16 Characters and Strings.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings, Pointers and Tools
Computer Programming for Engineers
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.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
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.
Topic 6 Recursion.
INC 161 , CPE 100 Computer Programming
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
CSE 1320 Search, Sort and Strings
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Functions Dr. Sajib Datta
Exercises on String Operations
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
Character Strings Lesson Outline
מחרוזות קרן כליף.
Computer Science 210 Computer Organization
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
INC 161 , CPE 100 Computer Programming
C Programming Strings.
Lecture 11 Strings.
CprE 185: Intro to Problem Solving (using C)
Incremental operators
EECE.2160 ECE Application Programming
Week 2 Variables, flow control and the Debugger
Chapter 8 Character Arrays and Strings
String manipulation string.h library
EECE.2160 ECE Application Programming
Exercise Arrays.
Introduction to Computer Organization & Systems
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings in C Array of characters is called a string.
Strings #include <stdio.h>
Programming Strings.
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Presentation transcript:

Strings (מחרוזות)

Example int main(void) { char str[] = "I'm a full string"; printf("%s\n", str); str[7] = 'o'; str[8] = 'o'; str[10] = '\0'; str[10] = 's'; return 0; } 0 1 2 3 4 5 6 7 8 9 10 11… I’m a full string I’m a fool string I’m a fool I’m a foolsstring

Example - Comparing Strings 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");

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

Equal strings – step by step ‘\0’ 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"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i 1

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i 1

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i 2

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i 2

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ 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"); B ‘Y’ ‘e’ ‘\0’ i 2

Exercise @ class 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 (“what”) with the second one (with) write a program to test the above function the program reads a string from the user and two characters, then call the function with the input, and print the result. example input: “papa”, ‘p’, ‘m’ output: “mama”

Example 1 Implement the library function strrchr (from string.h). Input - string str, char c. Output - the index of the last occurrence of c in str. Write a program that accepts a string and a char from the user and displays the index of its last occurrence.

my_strrchr int my_strrchr(char str[], char c) { int i; for (i=strlen(str)-1; i>=0; i--) if (str[i] == c) return i; return -1; {

Using my_strrchr char str[101]; char c; int last_index; printf("Please enter a string\n"); scanf("%100s",str); printf("Please enter a character\n"); scanf(" %c", &c); last_index = my_strrchr(str,c); if(last_index > -1) printf(“Last occurrence of %c in %s is in position %d\n", c, str, last_index); else printf("The letter %c was not found in %s\n",c,str);

Example 2 Implement the function my_strcspn: Input – two strings str1, str2 Output – the index in str1 of the first instance of any of the characters contained in str2 Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces

my_strcspn int my_strchr(char s[], char c) { int i = 0; while (s[i] != '\0') { if (s[i] == c) return i; i++; } return -1; int my_strcspn(char str[], char find[]) { int i = 0; while (str[i] != '\0') { if (my_strchr(find, str[i]) > -1) return i; i++; } return -1;

main char s[MAX_LENGTH+1]; int index; while (index > -1) { printf("Please enter a line of text\n"); scanf("%100s", s); index = my_strcspn(s, ".,;:!?"); while (index > -1) { s[index] = ' '; } printf("Resulting string is - %s\n", s);

Example 3 Write a program that gets a string from the user and checks whether it is a palindrome. A palindrome example: abbcbba (Hint: use strlen…)

Palindrome 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");

Example 4: Recursive strcmp Implement the function my_strcmp that compares two string recursively: Input – two strings str1, str2 Output – 0 if the string are identical, the lexicographical order otherwise Use a helper recursive function that holds the current index to be compared in the strings

Solution int my_strcmp(const char s[], const char t[]) { int myStrcmpRec(const char s[], const char t[], int i) { if (s[i] != t[i]) return s[i] - t[i]; if (s[i] == '\0') return 0; return my_strcmp(s, t, i+1); } int my_strcmp(const char s[], const char t[]) { return myStrcmpRec(s,t,0); }

Example 5: string2uint implement the function string2uint unsigned int string2uint(char str[]); The function scans a string that holds an positive integer and returns the corresponding unsigned integer Assume legal input How do we transform a digit-char to a digit-integer? Answer: -’0’

Example 5: string2uint (Cont.) write a program to test the above function read a string that represents an integer from the user call the function with the input print the result. example input: “1304”, output: 1304 input: “0560”, output: 560

Solution unsigned int string2uint(char str[]) { int i = 0, res = 0; while (str[i] != '\0') { res = res * 10 + (str[i] - '0'); ++i; } return res;

Solution – main (cont.) char str[SIZE+1]; unsigned int uint; printf("Please enter a string: "); scanf("%100s", str); uint = string2uint(str); printf("the number is %u\n",uint);

Holding Multiple Strings How would we hold multiple strings? Why? Students names, for example An array on strings!  A 2D array of characters: char str [NUM][MAX]; int i; for (i = 0; i < NUM; ++i) { printf("enter string %d:\n",i); scanf("%s",str[i]); }

Solution to class exercise 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; }

Solution (cont.) #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; }