freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

程序設(shè)計實習(xí)第三講字符串處理(已修改)

2024-10-27 14:08 本頁面
 

【正文】 程序設(shè)計實習(xí) 第三講 字符串處理 內(nèi)容提要 ? 字符串的存儲、字符串處理函數(shù) ? 將數(shù)組傳遞給函數(shù) ? 例題: ai2767 Caesar密碼 (P115) ? 例題:單詞排序 ? 例題: ai2744 子串 (P112) ? 例題: POJ1936 All in All 字符串 ? 每個字符串是一個特殊的數(shù)組,滿足兩個條件 ? 元素的類型為 char ? 最后一個元素的值為‘ \0? ,Ascii碼就是 0 ? 以字符型數(shù)組存儲 ? 從 0號元素開始存儲 ? 最大可以存儲長度為 N1的字符串, N是數(shù)組的大小。 ? 字符串“ hello”在長度為 10的字符串?dāng)?shù)組中的存儲 h e l l o \0 字符串處理函數(shù) ? 將格式化數(shù)據(jù)寫入字符串: sprintf ? 字符串長度查詢函數(shù): strlen ? 字符串復(fù)制函數(shù): strcpy、 strncpy ? 字符串連接函數(shù): strcat ? 字符串比較函數(shù): strcmp、 strncmp、 stricmp、strnicmp ? 字符串搜索函數(shù): strcspn、 strspn、 strstr、 strtok、strchr ? 字符串大小寫轉(zhuǎn)換函數(shù): strlwr、 strupr 這些函數(shù)都要求 include 把” hello world”復(fù)制到 str2 把 str2復(fù)制到 str1 查詢 str1中字符串的長度 字符串拷貝和求字符串長度 char *strcpy(char *dest, const char *src)。 int strlen(const char *s)。 include include void main() { char str1[10]=hello, str2[12]。 strcpy(str2,hello world)。 printf(length:%d(str1)。 %d(str2)\n, strlen(str1), strlen(str2))。 strcpy(str1, str2)。 printf(length:%d(str1)。 %d(str2)\n, strlen(str1), strlen(str2))。 printf(%s\n, str1)。 return。 } ? 輸出結(jié)果: length:5(str1)。 11(str2) length:11(str1)。 11(str2) hello world ? str1存儲了 11個非’ \0?字符? ? strcpy在復(fù)制字符串 str2到 str1時,不檢查 str2是否超出了 str1的存儲容量,而是直接將 str2中存儲的字符串復(fù)制到從 str1開始的一段連續(xù)區(qū)域 ? 在程序中要特別注意這種情況所引發(fā)的程序運行 不確定性 h e l l o \0 str1 str2 main() h e l l o \0 str1 h e l l o w o r l d \0 str2 strcpy(str2,“hello world)。 h e l l o str1 h e l l o w o r l d \0 str2 strcpy(str1,str2)。 w o r l d \0 用 strlen時常犯的錯誤 int MyStrchr(char * s, char c) //看 s中是否包含 c { for( int i = 0。 i strlen(s) 1 。 i ++ ) if( s[i] == c) return 1。 return 0。 } 哪里不好?這個函數(shù)執(zhí)行時間和 s 的長度是什么關(guān)系? strlen 是一個 o(N)的函數(shù),每次判斷 i strlen(s) – 1 都要執(zhí)行,太浪費時間了 include include void main() { char str1[100]=hello, str2[10]=^_^。 strcat(str1, world )。 printf(%s\n, str1)。 strcat(str1, str2)。 printf(%s\n, str1)。 return。 } 把” world”添加到 str1中原字符串的末尾 字符串添加 strcat 把 str2中的字符串添加到str1中原字符串的末尾 char *strcat(char *dest, const char *src)。 把 src內(nèi)容加到 dest后面,同樣不會考慮 dest是否夠長 輸出: hello world hello world ^_^ 字符串比較函數(shù) strcmp(分大小寫)、stricmp(不分大小寫) int strcmp(const char *s1, const char *s2)。 int stricmp(const char *s1, const char *s2)。 Return Value If s1 is... return value is... less than s2 0 the same as s2 == 0 greater than s2 0 Compares one string to anot
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
公安備案圖鄂ICP備17016276號-1