Lecture Introduction to computer and programming - Lecture No 22

In this section, we introduce the features of JML as they apply to the formal specification and verification of an individual function, such as the Factorial function that we specified and verified by hand in the previous section. We also show how JML allows us to specify run-time exceptions, providing a more robust vehicle than the pure Hoare triples in a real computational setting where exceptions actually occur. | CSC103: Introduction to Computer and Programming Lecture No 22 Previous lecture Strings Null character Memory map of string String initialization Display string content Input string from user Passing a string to a function Today’s lecture outline Standard library string functions strlen() strcpy() strcat() strcmp() Standard Library String Functions With every C compiler a large set of useful string handling library functions are provided strlen( ), to find length of string strcpy( ), to copy content of one string in another strcat( ) to append content of one string after the content of other string strcmp( ) to compare on string with another strlen( ) Short for string length This function counts the number of characters present in a string Prototype int strlen(char *); The base address of the string must be pass when calling strlen function char str1[ ] = “hello”; strlen(str1) call will return 5 not 6 \0 is not the part of string it is just character that tells where this string terminates h e l l o \0 65508 65509 65510 65511 65512 65513 str1[6] strlen() – program Go to program mystrlen( ) h e l l o \0 500 501 502 503 504 505 a[6] len . . . . . . *s 0 length 500 501 502 503 504 505 1 2 3 4 5 5 *s != ‘\0’ *(500) != ‘\0’ ‘h’ != ‘\0’ *s != ‘\0’ *(501) != ‘\0’ ‘e’ != ‘\0’ *s != ‘\0’ *(502) != ‘\0’ ‘l’ != ‘\0’ *s != ‘\0’ *(503) != ‘\0’ ‘l’ != ‘\0’ *s != ‘\0’ *(504) != ‘\0’ ‘o’ != ‘\0’ *s != ‘\0’ *(505) != ‘\0’ ‘\0’ != ‘\0’ true true true true true false Go to program strcpy( ) This function copies the contents of one string into another The base addresses of the source and target strings should be supplied to this function Prototype void strcpy (char *t, char *s); strcpy() - program Go to program mystrcpy( ) h e l l o \0 500 501 502 503 504 505 a[6] . . . . . . 600 601 602 603 604 605 b[6] *s 500 501 502 503 504 505 *t 600 601 602 603 604 605 *s != ‘\0’ *(500) != ‘\0’ ‘h’ != ‘\0’ *s != ‘\0’ *(501) != ‘\0’ ‘e’ != ‘\0’ *s != ‘\0’ *(502) != ‘\0’ ‘l’ != ‘\0’ *s != ‘\0’ . | CSC103: Introduction to Computer and Programming Lecture No 22 Previous lecture Strings Null character Memory map of string String initialization Display string content Input string from user Passing a string to a function Today’s lecture outline Standard library string functions strlen() strcpy() strcat() strcmp() Standard Library String Functions With every C compiler a large set of useful string handling library functions are provided strlen( ), to find length of string strcpy( ), to copy content of one string in another strcat( ) to append content of one string after the content of other string strcmp( ) to compare on string with another strlen( ) Short for string length This function counts the number of characters present in a string Prototype int strlen(char *); The base address of the string must be pass when calling strlen function char str1[ ] = “hello”; strlen(str1) call will return 5 not 6 \0 is not the part of string it is just character that tells where this string .

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.