做網(wǎng)站需要什么許可證推廣平臺哪個效果最好
目錄
Ⅰ、字符函數(shù)和字符串函數(shù)
????????1?.strlen
????????2.strcpy
????????3.strcat
????????4.strcmp
????????5.strncpy
????????6.strncat
????????7.strncmp
????????8.strstr
????????9.strtok
????????10.strerror
????????11.字符函數(shù)?
????????12. 字符轉換函數(shù)
Ⅱ、內存函數(shù)
? ? ? ? ?1 .memcpy
????????2.memmove
????????3.memcmp
Ⅰ、字符函數(shù)和字符串函數(shù)
????????1?.strlen
函數(shù)原型:
注意事項:
1.字符串以 ?'\0' 作為結束標志 , strlen 函數(shù)返回的是在字符串中 '\0' 前面 出現(xiàn)的字符個數(shù)( 不包含 '\0' ) 。2.參數(shù)指向的字符串 必須要以 '\0' 結束 。3.注意函數(shù)的 返回值 為 size_t ,是無符號的( 易錯 )4.學會 strlen 函數(shù)的模擬實現(xiàn)
模擬實現(xiàn):三種方法(計數(shù)器,遞歸,指針-指針)
//模擬實現(xiàn)strlen
//方法一:計數(shù)器
size_t my_strlen1(const char* str)
{char* st = str;size_t count = 0;while (*st++){count++;}return count;
}
//方法二:遞歸
size_t my_strlen2(const char* str)
{if (*str == '\0')return 0;return 1 + my_strlen2(++str);//注意這里不能用 后置++
}
//方法三:指針-指針
size_t my_strlen3(const char* str)
{char* st = str;while (*st != '\0'){//printf("%p\n", str);// 從小地址往大地址編址st++;}return st-str;
}
????????2.strcpy
函數(shù)原型:
注意事項:
1.Copies the C string pointed by source into the array pointed by destination, including theterminating null character (and stopping at that point).2.源字符串 必須以 '\0' 結束 。3.會將 源字符串 中的 '\0' 拷貝到 目標空間 。4.目標 空間必須足夠大 ,以確保能存放源字符串。5.目標 空間必須可變 。6.學會模擬實現(xiàn)。
模擬實現(xiàn):
//模擬實現(xiàn)strcpy
char* my_strcpy(char* dest, const char* src)
{assert(dest && src);char* cur = src; char *re = dest;while (*dest++ = *src++);return re;
}
????????3.strcat
函數(shù)原型:
注意事項:
1.Appends a copy of the source string to the destination string. The terminating null characterin destination is overwritten by the first character of source, and a null-character is includedat the end of the new string formed by the concatenation of both in destination.2.源字符串 必須以 '\0' 結束 。3.目標 空間 必須有 足夠 的大,能容納下源字符串的內容。4.目標空間必須 可修改 。5.字符串自己給自己追加,如何?不能,可能會 非法訪問
模擬實現(xiàn):
//模擬實現(xiàn)strcat
char* my_strcat(char* dest, const char* src)
{assert(dest&&src);char* ret = dest;while (*dest != '\0')dest++;char* sr = src;while (*dest++ = *sr++);return ret;
}
????????4.strcmp
函數(shù)原型:
注意事項:
1.This function starts comparing the first character of each string. If they are equal to eachother, it continues with the following pairs until the characters differ or until a terminatingnull-character is reached.標準規(guī)定:1.第一個字符串大于第二個字符串,則返回 大于0 的數(shù)字2.第一個字符串等于第二個字符串,則返回 03.第一個字符串小于第二個字符串,則返回 小于0 的數(shù)字
模擬實現(xiàn):
//模擬實現(xiàn)strcmp
int my_strcmp(const char* str1, const char* str2)
{assert(str1 && str2);while (*str1 == *str2){if (*str1 == '\0')return 0;str1++;str2++;}if (*str1 < *str2)return -1;elsereturn 1;
}
????????5.strncpy
函數(shù)原型:
注意事項:
1.Copies the first num characters of source to destination. If the end of the source C string(which is signaled by a null-character) is found before num characters have been copied,destination is padded with zeros until a total of num characters have been written to it.2.拷貝 num個字符 從 源 字符串到 目標空間 。3.如果源字符串的長度 小于num ,則拷貝完源字符串之后,在目標的后邊 追加0 ,直到 num 個
?簡單使用;
//使用strncpy
int main()
{char buf[128] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";char* str = "abcdef";printf("%s\n", strncpy(buf, str,7 ));//沒有超過就是n為多大就拷貝幾個過去,//n如果超過了字符串的長度后面就進行補 '\0'return 0;
}
執(zhí)行結果:
?
????????6.strncat
函數(shù)原型:
注意事項:
1.Appends the first num characters of source to destination, plus a terminating null-character.2.If the length of the C string in source is less than num, only the content up to the terminating null-character is copied3.如果 空間 不夠,要報錯4.如果num比源字符串長,則緊追加 NULL之前 的字符
簡單使用:
//使用strncat
int main()
{char buf[128]="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";buf[10] = '\0'; //從第一個'\0'開始拼接buf[20] = '\0';char* str = "abcdef";printf("%s", strncat(buf, str, 5));return 0;
}
執(zhí)行結果:
?
????????7.strncmp
函數(shù)原型:
注意事項:
比較到出現(xiàn)另個字符不一樣或者一個字符串結束或者num個字符全部比較完。
簡單使用:
/* strncmp example */
int main()
{char str[][5] = { "R2D2" , "C3PO" , "R2A6" };int n;puts("Looking for R2 astromech droids...");for (n = 0; n < 3; n++)if (strncmp(str[n], "R2xx", 2) == 0){printf("found %s\n", str[n]);}return 0;
}
?執(zhí)行結果:
????????8.strstr
函數(shù)原型:
注意事項:
Returns a poin ter to the first occurrence of str2 in str1, or a null pointer if str2 is not part ofstr1.
模擬實現(xiàn):
//strstr的模擬實現(xiàn)
char* my_strstr(const char* str1, const char* str2)
{while (*str1){if (*str1 == *str2){char* s1 = str1; char* s2 = str2;while (*s1 && *s2 && (*s1 == *s2)){s1++;s2++;}if (*s2 == '\0')return str1;}str1++;}
}
????????9.strtok
函數(shù)原型:
注意事項:
1.sep 參數(shù)是個字符串,定義了用作 分隔符的字符集合2.第一個參數(shù)指定一個字符串,它包含了 0個或者多個由sep字符串中一個或者多個分隔符 分割的標記。3.strtok 函數(shù) 找到str中的下一個標記 ,并將其用 \0 結尾 ,返回一個指向這個 標記的指針 。(注strtok函數(shù)會 改變被操作的字符串 ,所以在使用 strtok 函數(shù)切分的字符串一般都是 臨時拷貝的內容并且可修改 。)4.strtok 函數(shù)的 第一個參數(shù)不為 NULL ,函數(shù)將找到 str 中第一個標記, strtok 函數(shù)將 保存它在字符串中的位置 。5.strtok 函數(shù)的 第一個參數(shù)為 NULL ,函數(shù)將在同一個字符串中被 保存的位置開始 ,查找下一個標記。6.如果字符串中 不存在更多的標記 ,則 返回 NULL 指針 。
簡單使用:
//strtok使用
int main()
{char str[128] = "hyd@qwer.com.cn.edu.com";//注意這里必須是可修改的字符串,常量不能修改char* sep = "@.";char* ret = strtok(str, "@.");printf("%s\n", ret);while(ret=strtok(NULL,sep))printf("%s\n", ret);return 0;
}
執(zhí)行結果:
?
????????10.strerror
函數(shù)原型:
注意事項:
返回錯誤碼(errno),所對應的錯誤信息。
簡單使用:
//使用strerror
int main()
{FILE* fp = fopen("data.txt", "r");if (fp == NULL){//printf("打開失敗:%s\n", strerror(errno));//打印錯誤信息perror("打開失敗");//類似于 printf+錯誤原因}elseprintf("文件打開成功\n");return 0;
}
執(zhí)行結果:
????????11.字符函數(shù)?
函數(shù) 如果他的參數(shù)符合下列條件就返回真(非0)iscntrl ????????任何控制字符(1-31是控制字符,32-127是可打印字符)isspace ?????空白字符:空格‘ ’,換頁 ‘\f’ ,換行 '\n' ,回車 ‘\r’ ,制表符 '\t' 或者垂直制表符 '\v'isdigit ????????十進制數(shù)字 0~9isxdigit ??????十六進制數(shù)字,包括所有十進制數(shù)字,小寫字母a~f,大寫字母 A~Fislower ??????小寫字母a~zisupper ?????大寫字母A~Zisalpha? ? ? ?字母a~z 或 A~Zisalnum? ? ? 字母或者數(shù)字,a~z,A~Z,0~9ispunct? ? ? ?標點符號,任何不屬于數(shù)字或者字母的圖形字符(可打印)isgraph? ? ? ?任何圖形字符isprint? ? ? ? ?任何可打印字符,包括圖形字符和空白字符
????????12. 字符轉換函數(shù)
int tolower ( int c );int toupper? ( int c );
簡單使用:
/* isupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{int i=0;char str[]="Test String.\n";char c;while (str[i]){c=str[i];if (isupper(c)) c=tolower(c);putchar (c);i++;}return 0;
}
?執(zhí)行結果:
Ⅱ、內存函數(shù)
? ? ? ? ?1 .memcpy
函數(shù)原型:
注意事項:
1.函數(shù) memcpy 從 source 的位置開始向后復制 num 個字節(jié)的數(shù)據(jù)到 destination 的內存位置。2.這個函數(shù)在遇到 '\0' 的時候并不會停下來。3.如果 source 和 destination 有任何的重疊,復制的結果都是未定義的。
模擬實現(xiàn):
//模擬實現(xiàn)memcpy
char* my_memcpy(void* dest, const void* src,size_t sz)
{assert(dest && src);char* ret = dest;while (sz--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;src = (char*)src + 1;}return ret;
}
????????2.memmove
函數(shù)原型:
注意事項:
1.和 memcpy 的差別就是 memmove 函數(shù)處理的源內存塊和目標內存塊是可以重疊的。2.如果源空間和目標空間出現(xiàn)重疊,就得使用 memmove 函數(shù)處理。
模擬實現(xiàn):
//char* my_memmove(void* dest, const void* src, size_t sz)
{assert(dest && src);char* ret = (char*)dest;if (dest < src){while(sz--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;src = (char*)src + 1;}}else{while (sz--){*((char*)dest + sz) = *((char*)src + sz);}}
}
????????3.memcmp
函數(shù)原型:
注意事項;
1.比較從 ptr1 和 ptr2 指針開始的 num 個字節(jié)2.返回值如下:
簡單使用:
int main()
{int arr1[] = { 1,2,3,4,5,6,7 };//01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00//int arr2[] = { 1,2,3,0x11223304 };//01 00 00 00 02 00 00 00 03 00 00 00 04 33 22 11int ret = memcmp(arr1, arr2, 13);printf("%d\n", ret);return 0;
}
執(zhí)行結果:0?