void fillarrayint array int size int i srandtimeNULL

  • Slides: 25
Download presentation

 פתרון מלוי מערך void fill_array(int array[], int size) { int i; srand(time(NULL)); for

פתרון מלוי מערך void fill_array(int array[], int size) { int i; srand(time(NULL)); for (i = 0; i < size; i++) array[i] = rand()/327; }

 פתרון הדפסת מערך void print_array(int array[], int size) { int i; for (i

פתרון הדפסת מערך void print_array(int array[], int size) { int i; for (i = 0; i < size; i++) printf("%d ", array[i]); printf("n"); }

 פתרון הפיכת מערך void reverse_array(int array[], int size) { int i; for (i

פתרון הפיכת מערך void reverse_array(int array[], int size) { int i; for (i = 0; i < size/2; i++) swap(&array[i], &array[size - 1 - i]); }

#include <stdio. h> #include <stdlib. h> #include <time. h> #define SIZE 5 void fill_array(int

#include <stdio. h> #include <stdlib. h> #include <time. h> #define SIZE 5 void fill_array(int array[], int size); void reverse_array(int array[], int size); void print_array(int array[], int size); void swap(int *a, int *b); int main() { int my_arr[SIZE]; fill_array(my_arr, SIZE); print_array(my_arr, SIZE); reverse_array(my_arr, SIZE); print_array(my_arr, SIZE); return 0; }

 חשבון מצביעים reverse_array(my_arr, my_arr + SIZE -1); void reverse_array(int *begin, int *end) {

חשבון מצביעים reverse_array(my_arr, my_arr + SIZE -1); void reverse_array(int *begin, int *end) { while (begin < end) { swap(begin, end); begin++; אז int בגלל שהמצביע מסוג לפי הגודל 4 יודע לקדם ב ++ end--; . int של } } ae begin d r e a end

 הפיכה רקורסיבית של המערך reverse_array(my_arr, my_arr + SIZE -1); void reverse_array(int *begin, int

הפיכה רקורסיבית של המערך reverse_array(my_arr, my_arr + SIZE -1); void reverse_array(int *begin, int *end) { if (begin < end) { swap(begin, end); reverse_array(begin + 1, end - 1); } }

int *find. Sub. Array(int *array, int array_size, int *sub_array, int sub_size) { int i,

int *find. Sub. Array(int *array, int array_size, int *sub_array, int sub_size) { int i, j; for (i = 0; i < array_size; i++) { for (j = 0; j < sub_size; ++j) if ( *(array + i + j) != sub_array[j]) break; if (j == sub_size) return array+i; } return NULL; }

 קלט b 35 22 445 פלט ] &a[4 a 15 35 100 11

קלט b 35 22 445 פלט ] &a[4 a 15 35 100 11 22 445 3 1

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

#include <stdio. h> int strlen (const char * str) { const char *eos = str; while( *eos++ ) ; return( eos - str - 1 ); } int main() { char str[]="Liam"; printf("%d", strlen(str)); } ? מה קורה פה 100 101 102 103 104 105 106 a str r t w n ‘’ eos

int word_cnt (const char *s) { int cnt = 0; const char *next =

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

 מחרוזות Ø פונקציות המטפלות במחרוזות נמצאות בספרית string. h size_t strlen(const char *s);

מחרוזות Ø פונקציות המטפלות במחרוזות נמצאות בספרית string. h size_t strlen(const char *s); (return the length of the string s, not counting the terminating null character) char *strcpy(char *s 1, const char *s 2); (the programmer must ensure that s 1 points to enough space for result) Ø char *strcat(char *s 1, const char *s 2); (the programmer must ensure that s 1 points to enough space for result) Ø Ø Ø int strcmp(const char *s 1, const char *s 2); int strcnmp(const char *s 1, const char *s 2, size_t n); lexicographic comparison Return value is less than, equal to, or greater than zero according comparison Strncmp - compares at most n characters

/* Implementation of string library functions */ #include <stdio. h> int strcmp(const char *s

/* Implementation of string library functions */ #include <stdio. h> int strcmp(const char *s 1, const char *s 2) { int k = 0; while(s 1[k] == s 2[k] && s 1[k]) { k++; } return (s 1[k] - s 2[k]); } /* Implementation of string library functions */ #include <stdio. h> char *strcpy(char *s 1, const char *s 2) { char *head = s 1; while(*s 2) { *s 1++ = *s 2++; } return head; }

#include <stdio. h> #define STR_SIZE 256 #define UPPER 'A' - 'a' char *To. Upper(char

#include <stdio. h> #define STR_SIZE 256 #define UPPER 'A' - 'a' char *To. Upper(char * str); int main() { char str[STR_SIZE]; gets(str); printf("%sn", To. Upper(str)); return 0; } char *To. Upper(char * str) { char *ps = str; while(*ps) { if (*ps >= 'a' && *ps <= 'z') *ps += UPPER; ps++; } return str; }

include <stdio. h> char* strstr ( char * str 1, char * str 2)

include <stdio. h> char* strstr ( char * str 1, char * str 2) { char *cp = str 1; char *s 1, *s 2; if ( !*str 2 ) return(str 1); while (*cp) { s 1 = cp; s 2 = str 2; while ( *s 1 && *s 2 && !(*s 1 -*s 2) ) s 1++, s 2++; if (!*s 2) return(cp); cp++; } return(NULL); בדיקת תת המחרוזת } int main() { char s 1[]="I am a boy"; char s 2[]="am"; printf("%s", strstr(s 1, s 2)); }