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

Preview:

Citation preview

99 תכנות תרגולתכנות תרגול

::שבועשבוע

2525..1212.05.05

הקשר בין מערכים למצביעיםהקשר בין מערכים למצביעיםנרצה לעמוד על הקשר בין מערך למצביענרצה לעמוד על הקשר בין מערך למצביע

מאחר ומערכים הם הכללה של משתנים הרי שברור מאחר ומערכים הם הכללה של משתנים הרי שברור שלמערך ולכל אחד מאיבריו יש כתובת שלמערך ולכל אחד מאיבריו יש כתובת

a[0]

a[1]

a[2]

a[9]

5

5000

5004

5008

כדי להגיע לכתובת של תא כדי להגיע לכתובת של תא a[2]a[2]&&במערך פשוט נכתוב במערך פשוט נכתוב

של של הכתובתהכתובתאם נרצה את אם נרצה את המערך עצמו אזי זה שם המערך עצמו אזי זה שם

המערךהמערך

הקשר בין מערכים לפונקציותהקשר בין מערכים לפונקציותכיצד נכתוב פונקציה אשר תקבל כפרמטר מערך ? כיצד נכתוב פונקציה אשר תקבל כפרמטר מערך ?

הפונקציה תקבל מערך )את כתובתו( ותדפיס אותו:הפונקציה תקבל מערך )את כתובתו( ותדפיס אותו:void PrintArray(int a[],int size)void PrintArray(int a[],int size){{

int i;int i;for (i=0;i<size;i++)for (i=0;i<size;i++)

printf(“%d”,a[i]);printf(“%d”,a[i]);}}

הקשר בין מערכים לפונקציותהקשר בין מערכים לפונקציות

הפונקציה תקלוט איברים לתוך מערךהפונקציה תקלוט איברים לתוך מערך

void GetArray(int a[],int size)void GetArray(int a[],int size){{

int i;int i;for (i=0;i<size;i++)for (i=0;i<size;i++)

scanf(“%d”,&a[i]);scanf(“%d”,&a[i]);}}

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

חשבון מצביעיםחשבון מצביעים

a[0]

a[1]

a[2]

a[9]

5

5000

5004

5008

כדי להגיע לתא השלישי כדי להגיע לתא השלישי a[2]a[2]במערך נוכל לעשות במערך נוכל לעשות

או או

*(a+2)*(a+2)

void reverse_array(int *begin, int *end){

while (begin < end){

swap(begin, end);begin++;end--;

}}

reverse_array(my_arr, my_arr + SIZE -1);חשבון מצביעיםחשבון מצביעים

begin end

3 11 35 7

מחרוזותמחרוזותמחרוזת זה מערך של תווים אשר מחרוזת זה מערך של תווים אשר מחרוזת – מחרוזת –

. . ’’00‘\‘\מסתיים ב מסתיים ב

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%הדפסה באמצעות

מחרוזותמחרוזות מאפשר לנו להעביר מחרוזות מאפשר לנו להעביר מחרוזות ’’00‘\‘\ הסיום בהסיום ב

לפונקציות מבלי לציין את גודלם!!!לפונקציות מבלי לציין את גודלם!!!פונקציה טיפוסית של מחרוזות תראה כך:פונקציה טיפוסית של מחרוזות תראה כך:

void string_func(char *s){…

while (*s != ’\0’)…{

דרכים נוספות להגדיר מחרוזותדרכים נוספות להגדיר מחרוזות

h

p

e l l o \0 h e l l o \0s

char *p = “hello” char *p = “hello” char s[ ] = char s[ ] = “hello”“hello”

קלט למחרוזתקלט למחרוזת

scanf(“%s”, str); scanf(“%s”, str); gets(str);gets(str);

עד לרווח

עד לאנטר

#include <stdio.h>#include <stdio.h>int strlen(char *str)int strlen(char *str){{ charchar *eos;*eos;

eos = str;eos = str; while ( *eos != ‘\0’ )while ( *eos != ‘\0’ )

eos++;eos++; return eos - str;return eos - str;}}void main()void main(){{

char str[]="LINUX";char str[]="LINUX";printf("%d\n",strlen(str));printf("%d\n",strlen(str));

}}

N U XIL ‘\0’

str

eos

100 101 102 103 104105 106

Pointer arithmetic strcpyPointer arithmetic strcpy

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘!’ ‘!’ ‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{while (*src != '\0')while (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘!’ ‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘!’ ‘!’ ‘!’ ‘!’

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘!’ ‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘!’ ‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{while (*src != '\0')while (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘!’ ‘!’ ‘!’

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘!’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{while (*src != '\0')while (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘s’

‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘s’ ‘!’ ‘!’

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘s’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘s’ ‘!’ ‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{while (*src != '\0')while (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

Strcpy – step by stepStrcpy – step by step

‘y’

‘e’

‘s’ ‘\0’

‘%’

src

dest

‘y’

‘e’

‘s’ ‘\0’

‘!’

voidvoid my_strcpy( my_strcpy(charchar *dest, *dest, charchar *src) *src){{whilewhile (*src != '\0') (*src != '\0'){{

*dest = *src;*dest = *src;dest++;dest++;src++;src++;

} } *dest = '\0';*dest = '\0';

}}

ExerciseExercise

• Write a function with the prototype:Write a function with the prototype:voidvoid replace_char( replace_char(charchar *str, *str,

charchar c1, c1, charchar c2); c2);

• It replaces each appearance ofIt replaces each appearance of c1 c1 by by c2c2 in in the string the string str. str. Do not use theDo not use the [] [] operator!operator!

• Demonstrate your function with a program Demonstrate your function with a program that uses itthat uses it

פתרוןפתרון

void replace_char(char *str, char c1, char c2)void replace_char(char *str, char c1, char c2){{

while (*str != '\0')while (*str != '\0'){{

if (*str == c1)if (*str == c1)*str = c2;*str = c2;

++str;++str;}}

}}

#include <stdio.h>#include <stdio.h>void replace_char(char *str, char c1, char c2);void replace_char(char *str, char c1, char c2);int main()int main(){{

char my_str[101], replace_what_letter, char my_str[101], replace_what_letter, replace_with_letter;replace_with_letter;printf("Please enter a string (no spaces)\n");printf("Please enter a string (no spaces)\n");scanf("%100s", my_str);scanf("%100s", my_str);

printf("Letter to replace: ");printf("Letter to replace: ");scanf(" %c", &replace_what_letter);scanf(" %c", &replace_what_letter);printf("Letter to replace with: ");printf("Letter to replace with: ");scanf(" %c", &replace_with_letter);scanf(" %c", &replace_with_letter);replace_char(my_str, replace_what_letter, replace_char(my_str, replace_what_letter, replace_with_letter);replace_with_letter);printf("The result:\n%s\n", my_str);printf("The result:\n%s\n", my_str);return 0;return 0;

}}

11תרגיל תרגיל כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם

הן זהות.הן זהות...00. אם לא, אז יוחזר . אם לא, אז יוחזר 11 אם כן, הפונקציה תחזיר אם כן, הפונקציה תחזיר

תווים תווים 1010הערה: יש להגביל אורך כל מחרוזת ל-הערה: יש להגביל אורך כל מחרוזת ל-באופן הבא:באופן הבא:

char s[11];char s[11];scanf(“%10s”,s);scanf(“%10s”,s);

אם רוצים שהמחרוזת תכיל גם רווחים:אם רוצים שהמחרוזת תכיל גם רווחים:gets(s);gets(s);if ( strlen(s) > 0 && s[strlen(s)-1] == ‘\if ( strlen(s) > 0 && s[strlen(s)-1] == ‘\

n’ )n’ )s[strlen(s)-1] = ‘\0’;s[strlen(s)-1] = ‘\0’;

#include <stdio.h>#include <stdio.h>int compare (char s1[ ],char s2[ ])int compare (char s1[ ],char s2[ ]){{

intint k;k;

for ( k=0; s1[k] != ‘\0’ || s2[k] != ‘\0’; k++ )for ( k=0; s1[k] != ‘\0’ || s2[k] != ‘\0’; k++ )if ( s1[k] != s2[k] )if ( s1[k] != s2[k] )

return 0;return 0;

return 1;return 1;}}void main()void main(){{

charchar s1[11],s2[11];s1[11],s2[11];scanf(“%10s%10s”,s1,s2);scanf(“%10s%10s”,s1,s2);printf(“%d\n”,compare(s1,s2));printf(“%d\n”,compare(s1,s2));

}}

22תרגיל תרגיל

כתבו פונקציה המקבלת שתי מחרוזות ובודקת כתבו פונקציה המקבלת שתי מחרוזות ובודקת האם מחרוזת אחת נמצאת כתת מחרוזת האם מחרוזת אחת נמצאת כתת מחרוזת

במחרוזת השנייה.במחרוזת השנייה.

אם כן, תחזיר מצביע למקום זה. אם כן, תחזיר מצביע למקום זה... NULLNULLאם לא, אז יוחזר אם לא, אז יוחזר

#include <stdio.h>#include <stdio.h>char *strstr ( char * str1, char * str2)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++; }{ s1++; s2++; } if ( *s2 == ‘\0’ )if ( *s2 == ‘\0’ ) return cp;return cp; cp++;cp++; }} return NULL;return NULL;}}void main()void main(){{

char s1[]="I am a boy";char s1[]="I am a boy";char s2[]="am";char s2[]="am";printf("%s\n",strstr(s1,s2));printf("%s\n",strstr(s1,s2));

}}

בדיקת תת המחרוזת

string.hstring.h הספריההספריה

מכילה בין השאר את הפונקציות הבאות לטיפול מכילה בין השאר את הפונקציות הבאות לטיפול במחרוזות:במחרוזות:

•strlen(s)strlen(s) מחזירה את האורך של המחרוזת – מחזירה את האורך של המחרוזת – ss..•strcpy(s,t)strcpy(s,t) מעתיקה את המחרוזת – מעתיקה את המחרוזת – tt-ל- ל ss..•strcmp(s,t)strcmp(s,t) משווה ביו המחרוזות – משווה ביו המחרוזות – ss-ו- ו tt..•strstr(s,t)strstr(s,t) מחפשת את המחרוזת – מחפשת את המחרוזת – tt בתוך המחרוזת בתוך המחרוזת

ss..

33תרגיל תרגיל

כתבו פונקציה המקבלת כקלט מחרוזת ובודקת כתבו פונקציה המקבלת כקלט מחרוזת ובודקת האם היא פולינדרום.האם היא פולינדרום.

100100הערה: יש להגביל את אורך המחרוזת ל-הערה: יש להגביל את אורך המחרוזת ל-תווים. תווים.

#include <stdio.h>#include <stdio.h>#include <string.h>#include <string.h>int main()int main(){{

charchar s[101];s[101];intint k,len;k,len;scanf(“%100s”,s);scanf(“%100s”,s);len = strlen(s);len = strlen(s);for ( k=0; k<len/2; k++ )for ( k=0; k<len/2; k++ )

if ( s[k] != s[len-1-k] )if ( s[k] != s[len-1-k] ){{

printf(“The string is not a palindrome!\printf(“The string is not a palindrome!\n”);n”);

return 0;return 0;}}

printf(“The string is a palindrome!\n”);printf(“The string is a palindrome!\n”);return 0;return 0;

}}

44תרגיל תרגיל

כתבו פונקציה לחישוב כמה מילים נמצאות בתוך כתבו פונקציה לחישוב כמה מילים נמצאות בתוך מחרוזת. מחרוזת.

המילים מופרדות על ידי רווחים. המילים מופרדות על ידי רווחים.רמז:רמז: isspace(char c)isspace(char c)ניתן להשתמש בפונקציה ניתן להשתמש בפונקציה

שנמצאת שנמצאת <<include <ctype.hinclude <ctype.h##ב ב

int word_cnt (char *s)int word_cnt (char *s){{

intint cnt = 0;cnt = 0;charchar *next = s + 1;*next = s + 1;if (*s == '\0') if (*s == '\0') // empty string// empty string

return 0;return 0;while(*next != '\0')while(*next != '\0'){{

if (!isspace(*s) && isspace(*next))if (!isspace(*s) && isspace(*next))cnt++;cnt++;

s++;s++;next++;next++;

}}if (!isspace(*s))if (!isspace(*s))

cnt++;cnt++;return cnt;return cnt;

}}

Recommended