一.字符串分類函數
頭文件:ctype.h
作用:判斷是什么類型的字符
函數舉例:
函數 | 符合條件就為真 |
islower | 判斷是否為小寫字符(a~z) |
isupper | 判斷是否為大寫字符(A~Z) |
isdigit | 十進制數字,(0~9)字符 |
isxdigit | 16進制數字,十進制數字字符,大寫A~Z,小寫a~z |
isspace | 空白字符(空白換行等) |
iscntrl | 任何控制字符 |
isalpha | 字母a~z,A~Z |
isalnum | 字母和數字 |
ispunct | 標點符號 |
isgrape | 任何圖形字符 |
isprint | 任何可打印字符 |
應用舉例:
代碼1(islower):
#include <stdio.h> //將字符串中的小寫轉換為大寫(普通)
#include <ctype.h>
int main()
{int i = 0;char arr[] = "I am from China";while (arr[i]){if (islower(arr[i]))arr[i] -= 32;printf("%c", arr[i]);i++;}return 0;
}
運行結果:
二.字符轉化函數
1.tolower
將大寫轉化為小寫
2.toupper
將小寫轉化為大寫
應用:
代碼:
#include <stdio.h>
#include <ctype.h>
int main()
{int i = 0;char arr[] = "I am from China";while (arr[i]){if (islower(arr[i]))arr[i] = toupper(arr[i]);printf("%c", arr[i]);i++;}return 0;
}
運行結果:
三.strlen的返回值
返回值為size_t類型的,為無符號類型(>=0)
舉例:
#include <stdio.h>
#include <string.h>
int main()
{int arr[] = "hjf";int arr2[] = "hjfjdf";if ((strlen(arr) - strlen(arr2)) > 0)printff(">");elseprintff("<=");return 0;
}
結果和解釋:
1.strlen的模擬和實現(遞歸)
代碼:
#include <stdio.h>
size_t my_strlen(char* pa)
{if (*pa == 0)return 0;elsereturn 1 + my_strlen(pa + 1);
}
int main()
{char arr2[] = "hjfjdf";printf("%zu", my_strlen(arr2));return 0;
}
結果:
四.strcpy
作用:字符串拷貝,拷貝到源頭的‘\0’為止
source:指針,指向源頭數據
destination:指針,指向目的地空間,拷貝源頭數據包括‘\0’
返回值:destination的首元素地址
頭文件:string.h
注意:目的地空間必須足夠大
舉例:
代碼:
#include <stdio.h>
#include <string.h>
int main()
{char arr2[] = "hello";char arr1[] = "world";printf("%s", strcpy(arr2, arr1));return 0;
}
結果:
模擬和實現:
代碼:
#include <stdio.h>
char* my_strcpy(char* des, const char* sou)
{char* ret = des;while (*des++ = *sou++){;}return ret;
}
#include <string.h>
int main()
{char arr2[] = "hello";char arr1[] = "world";printf("%s", my_strcpy(arr2, arr1));return 0;
}
結果:
五.strcat
與strcat參數、返回值一樣
作用:把source的字符串追加到destination后
舉例:
代碼:
#include <stdio.h>
#include <string.h>
int main()
{char arr2[20] = "hello";char arr1[] = "world";printf("%s", strcat(arr2, arr1));return 0;
}
結果:
模擬和實現:
代碼:
#include <stdio.h>
char* my_strcat(char* des, const char* sou)
{char* re = des;while (*des){des++;}while (*des++ = *sou++){;}return re;
}
int main()
{char arr2[20] = "hello";char arr1[] = "world";printf("%s", my_strcat(arr2, arr1));return 0;
}
結果和解釋:
六.strcmp(補充指針5)
模擬實現:
代碼:
#include <stdio.h>
int my_strcmp(char* str1, char* str2)
{while (*str1 == *str2){if (*str1 == '\0')return 0;str1++;str2++;}return *str1 - *str2;
}
int main()
{char arr[] = "abcd";char arr1[] = "ab";int ret = my_strcmp(arr, arr1);printf("%d", ret);return 0;
}
結果:
七.strncpy
形式:
功能:字符串拷貝,與strcpy相比,可以控制拷貝字符串數量
應用:
八.strncat
形式:
功能:與strcat相比,可以控制追加字符數量
應用:
九.strncmp
功能:與strncmp相比,可以控制比較字符的數量
十.strstr
形式:
功能:查str2指向的字符串在str1指向的字符串中第一次出現的位置
返回值:
如果找到了,返回第一次出現位置的指針
沒找到返回空指針
應用:
模擬實現:
代碼:
#include <stdio.h>
const char* my_strstr(const char* str1, const char* str2)
{char* p = str1;while (*p){char* S1 =p;char* S2 = str2;while (*S1 && *S2 && *S1==*S2){S1++;S2++;}if (*S2 == '\0')return p;p++;}return NULL;
}
int main()
{char arr[] = "abcdefccdef";char arr1[20] ="def";printf("%s",my_strstr(arr,arr1));return 0;
}
結果:
十一.strtok
形式:
功能:
1.分割字符串,根據delimiters參數中的分隔符將字符串分成多個子字符串
2.根據delimiters參數中的分格符會插入’\0‘替換分隔符的位置
參數:
str:??首次傳入帶分割的字符串,后續傳NULL,表示繼續分割同一個字符串
delimiters:分隔符
返回值:
成功時返回當前指向的字符串
沒有更多字符串時返回NULL
應用:
代碼:
#include <stdio.h>
#include <string.h>
int main()
{char arr[] = "192.110...211.231";char arr1[20] =".";for (char* p = strtok(arr, arr1);p != NULL;p = strtok(NULL, arr1)){printf("%s\n", p);}return 0;
}
結果: