Presentation is loading. Please wait.

Presentation is loading. Please wait.

תכנות תרגול 9 שבוע : 25.12.05. הקשר בין מערכים למצביעים נרצה לעמוד על הקשר בין מערך למצביע מאחר ומערכים הם הכללה של משתנים הרי שברור שלמערך ולכל אחד מאיבריו.

Similar presentations


Presentation on theme: "תכנות תרגול 9 שבוע : 25.12.05. הקשר בין מערכים למצביעים נרצה לעמוד על הקשר בין מערך למצביע מאחר ומערכים הם הכללה של משתנים הרי שברור שלמערך ולכל אחד מאיבריו."— Presentation transcript:

1 תכנות תרגול 9 שבוע : 25.12.05

2 הקשר בין מערכים למצביעים נרצה לעמוד על הקשר בין מערך למצביע מאחר ומערכים הם הכללה של משתנים הרי שברור שלמערך ולכל אחד מאיבריו יש כתובת a[0] a[1] a[2] a[9]  5 5000 5004 5008 כדי להגיע לכתובת של תא במערך פשוט נכתוב &a[2] אם נרצה את הכתובת של המערך עצמו אזי זה שם המערך

3 הקשר בין מערכים לפונקציות כיצד נכתוב פונקציה אשר תקבל כפרמטר מערך ? הפונקציה תקבל מערך ( את כתובתו ) ותדפיס אותו : void PrintArray(int a[],int size) { int i; for (i=0;i<size;i++) printf(“%d”,a[i]);}

4 הקשר בין מערכים לפונקציות הפונקציה תקלוט איברים לתוך מערך void GetArray(int a[],int size) { int i; for (i=0;i<size;i++) scanf(“%d”,&a[i]);}

5 void PrintArray(int a[],int size); int main() { int a[SIZE]; GetArray(a,SIZE); PrintArray(a,SIZE); return 0; } void GetArray(int a[],int size);

6 חשבון מצביעים a[0] a[1] a[2] a[9]  5 5000 5004 5008 כדי להגיע לתא השלישי במערך נוכל לעשות a[2] או*(a+2)

7 void reverse_array(int *begin, int *end) { while (begin < end) { swap(begin, end); begin++; end--; } reverse_array(my_arr, my_arr + SIZE -1); חשבון מצביעים beginend 31 1 35 7

8 מחרוזות מחרוזת – מחרוזת זה מערך של תווים אשר מסתיים ב ‘\0’. a[0] a[1] a[2] a[3] 5000 5001 5002 a[4] a[5] 5003 5004 5005 ‘S’ ‘h’ ‘a’ ‘i’ ‘\0’ char a[6] = “Shai”; printf(“%s”,a); הדפסה באמצעות %s

9 מחרוזות הסיום ב ‘\0’ מאפשר לנו להעביר מחרוזות לפונקציות מבלי לציין את גודלם !!! פונקציה טיפוסית של מחרוזות תראה כך : void string_func(char *s) { … while (*s != ’\0’) … {

10 דרכים נוספות להגדיר מחרוזות h p ello\0 hello s char *p = “hello” char s[ ] = “hello” קלט למחרוזת scanf(“%s”, str); gets(str); עד לרווח עד לאנטר

11 #include #include int strlen(char *str) { char*eos; char*eos; eos = str; while ( *eos != ‘\0’ ) while ( *eos != ‘\0’ )eos++; return eos - str; return eos - str;} void main() { char str[]="LINUX"; printf("%d\n",strlen(str));} NUXIL‘\0’ str eos 100101102 103104105 106

12 Pointer arithmetic strcpy void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

13 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

14 void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; } Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

15 void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; } Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

16 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

17 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

18 void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; } Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

19 void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; } Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

20 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

21 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

22 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

23 void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; } Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’

24 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

25 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

26 Strcpy – step by step ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘\0’‘!’ void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++;src++;} *dest = '\0'; }

27 Exercise Write a function with the prototype: Write a function with the prototype: void replace_char(char *str, char c1, char c1, char c2); char c2); It replaces each appearance of c1 by c2 in the string str. Do not use the [] operator! It replaces each appearance of c1 by c2 in the string str. Do not use the [] operator! Demonstrate your function with a program that uses it Demonstrate your function with a program that uses it

28 פתרון void replace_char(char *str, char c1, char c2) { while (*str != '\0') { if (*str == c1) *str = c2; ++str;}}

29 #include #include void replace_char(char *str, char c1, char c2); int main() { char my_str[101], replace_what_letter, replace_with_letter; printf("Please enter a string (no spaces)\n"); scanf("%100s", my_str); printf("Letter to replace: "); printf("Letter to replace: "); scanf(" %c", &replace_what_letter); printf("Letter to replace with: "); scanf(" %c", &replace_with_letter); replace_char(my_str, replace_what_letter, replace_with_letter); printf("The result:\n%s\n", my_str); return 0; }

30 תרגיל 1 כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם הן זהות. אם כן, הפונקציה תחזיר 1. אם לא, אז יוחזר 0. אם כן, הפונקציה תחזיר 1. אם לא, אז יוחזר 0. הערה : יש להגביל אורך כל מחרוזת ל -10 תווים באופן הבא : char s[11]; scanf(“%10s”,s); אם רוצים שהמחרוזת תכיל גם רווחים : gets(s); if ( strlen(s) > 0 && s[strlen(s)-1] == ‘\n’ ) s[strlen(s)-1] = ‘\0’;

31 #include #include int compare (char s1[ ],char s2[ ]) { intk; for ( k=0; s1[k] != ‘\0’ || s2[k] != ‘\0’; k++ ) if ( s1[k] != s2[k] ) return 0; return 1; } void main() { chars1[11],s2[11]; scanf(“%10s%10s”,s1,s2);printf(“%d\n”,compare(s1,s2));}

32 תרגיל 2 כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם מחרוזת אחת נמצאת כתת מחרוזת במחרוזת השנייה. כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם מחרוזת אחת נמצאת כתת מחרוזת במחרוזת השנייה. אם כן, תחזיר מצביע למקום זה. אם כן, תחזיר מצביע למקום זה. אם לא, אז יוחזר NULL.

33 #include #include char *strstr ( char * str1, char * str2) { char *cp = str1; char *cp = str1; char *s1, *s2; char *s1, *s2; if ( *str2 == ‘\0’ ) if ( *str2 == ‘\0’ ) return str1; return str1; while ( *cp != ‘\0’ ) while ( *cp != ‘\0’ ) { s1 = cp; s1 = cp; s2 = str2; s2 = str2; while ( *s1 != ‘\0’ && *s2 != ‘\0’ && *s1 == *s2 ) while ( *s1 != ‘\0’ && *s2 != ‘\0’ && *s1 == *s2 ) { s1++; s2++; } if ( *s2 == ‘\0’ ) if ( *s2 == ‘\0’ ) return cp; return cp; cp++; cp++; } return NULL; return NULL;} void main() { char s1[]="I am a boy"; char s2[]="am"; printf("%s\n",strstr(s1,s2));} בדיקת תת המחרוזת

34 string.h הספריה מכילה בין השאר את הפונקציות הבאות לטיפול במחרוזות : strlen(s) – מחזירה את האורך של המחרוזת s. strlen(s) – מחזירה את האורך של המחרוזת s. strcpy(s,t) – מעתיקה את המחרוזת t ל -s. strcpy(s,t) – מעתיקה את המחרוזת t ל -s. strcmp(s,t) – משווה ביו המחרוזות s ו -t. strcmp(s,t) – משווה ביו המחרוזות s ו -t. strstr(s,t) – מחפשת את המחרוזת t בתוך המחרוזת s. strstr(s,t) – מחפשת את המחרוזת t בתוך המחרוזת s.

35 תרגיל 3 כתבו פונקציה המקבלת כקלט מחרוזת ובודקת האם היא פולינדרום. הערה : יש להגביל את אורך המחרוזת ל -100 תווים.

36 #include #include int main() { chars[101]; intk,len; scanf(“%100s”,s); len = strlen(s); for ( k=0; k<len/2; k++ ) if ( s[k] != s[len-1-k] ) { printf(“The string is not a palindrome!\n”); return 0; } printf(“The string is a palindrome!\n”); return 0; }

37 תרגיל 4 כתבו פונקציה לחישוב כמה מילים נמצאות בתוך מחרוזת. רמז : המילים מופרדות על ידי רווחים. ניתן להשתמש בפונקציה isspace(char c) שנמצאת ב #include ב #include

38 int word_cnt (char *s) { intcnt = 0; char*next = s + 1; if (*s == '\0') // empty string return 0; while(*next != '\0') { if (!isspace(*s) && isspace(*next)) cnt++;s++;next++;} if (!isspace(*s)) cnt++; return cnt; }


Download ppt "תכנות תרגול 9 שבוע : 25.12.05. הקשר בין מערכים למצביעים נרצה לעמוד על הקשר בין מערך למצביע מאחר ומערכים הם הכללה של משתנים הרי שברור שלמערך ולכל אחד מאיבריו."

Similar presentations


Ads by Google