目錄:
- 1.字元串函數strlen
-
- (1)strlen函數
- (2)strlen的使用
-
- a.代碼
- b.運作結果
- (3)模拟實作strlen函數
-
- a.代碼
- b.運作結果
- (4)注意:
- 2.字元串函數strcpy
-
- (1)strcpy函數
- (2)strcpy的使用
-
- a.代碼
- b.運作結果
- c.錯誤舉例
- (3)模拟實作strcpy
-
- a.代碼
- 3.字元串函數strcat
-
- (1)strcat函數
- (2)strcat的使用
-
- a.代碼
- b.運作結果
- (3)模拟實作strcat
-
- a.代碼
- b.運作結果
- 4.字元串函數strcmp
-
- (1)strcmp函數
- (2)strcmp的使用
-
- a.代碼
- b.運作結果
- (3)模拟實作strcmp
-
- a.代碼
- b.運作結果
1.字元串函數strlen
(1)strlen函數
strlen函數傳回的是在字元串中’\0’前面出現的字元的個數
(2)strlen的使用
a.代碼
#include<stdio.h>
#include<string.h>
int main()
{
char str1[] = "abcdef";
printf("%d\n", strlen(str1));
return 0;
}
b.運作結果
(3)模拟實作strlen函數
a.代碼
#include<stdio.h>
#include<string.h>
size_t MyStrlen(const char* str1)
{
size_t len = 0;
while (*str1 != 0)
{
++len;
++str1;
}
return len;
}
int main()
{
char* str1 = "abcdef";
printf("%d\n", MyStrlen(str1));
return 0;
}
b.運作結果
(4)注意:
a.指針在傳遞(指派,傳參數)的過程中,權限不能放大,隻能縮小
b.typedef unsigned int size_t
size_t就是無符号的整型
2.字元串函數strcpy
(1)strcpy函數
strcpy是覆寫拷貝,将source全覆寫拷貝到destination,會把’\0’也拷過去,且必須考慮destination的空間夠不夠
(destination的空間必須>=source的空間)
(2)strcpy的使用
a.代碼
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
char p1[] = "abcdef";
char* p2 = "hello";
strcpy(p1, p2);
printf("%s\n", p1);
printf("%s\n", p2);
return 0;
}
b.運作結果
c.錯誤舉例
代碼:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
char p1[] = "abcdef";
char* p2 = "hello";
const char* p3 = "world";
strcpy(p2, p3);
printf("%s\n", p2);
printf("%s\n", p3);
return 0;
}
運作結果:
錯誤分析:
(3)模拟實作strcpy
a.代碼
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void MyStrcpy(char * dst, const char * src)
{
while (*src)
{
*dst = *src;
++src;
++dst;
}
*dst = '\0';
}
int main()
{
char p1[] = "abcdef";
const char* p2 = "hello";
MyStrcpy(p1, p2);
printf("%s\n", p1);
printf("%s\n", p2);
return 0;
}
b.運作結果
3.字元串函數strcat
(1)strcat函數
strcat追加拷貝,追加到目标空間後面,目标空間必須足夠大,能容納下源字元串的内容
(2)strcat的使用
a.代碼
#include<stdio.h>
#include<string.h>
int main()
{
char p1[20] = "hello";
const char* p2 = " world";
strcat(p1, p2);
printf("%s\n",p1);
return 0;
}
b.運作結果
(3)模拟實作strcat
a.代碼
#include<stdio.h>
#include<string.h>
void MyStrcat(char* dst, const char * src)
{
//讓dst指向'\0'位置
while (*dst != '\0')
{
++dst;
}
//讓dst從'\0'開始,将src指派給dst
while (*dst = *src)
{
++dst;
++src;
}
*dst = '\0';
}
int main()
{
char p1[20] = "hello";
const char* p2 = " world";
MyStrcat(p1, p2);
printf("%s\n",p1);
return 0;
}
b.運作結果
4.字元串函數strcmp
(1)strcmp函數
strcmp比較兩個字元串的大小,一個字元一個字元比較,按ASCLL碼比較
标準規定:
第一個字元串大于第二個字元串,則傳回大于0的數字
第一個字元串等于第二個字元串,則傳回0
第一個字元串小于第二個字元串,則傳回小于0的數字
(2)strcmp的使用
a.代碼
#include<stdio.h>
#include<string.h>
int main()
{
char* p1 = "abcdef";
char* p2 = "abcdef";
char* p3 = "abcd";
char* p4 = "bcde";
printf("%d\n", strcmp(p1,p2 ));
printf("%d\n", strcmp(p1,p3 ));
printf("%d\n", strcmp(p3,p4 ));
}
b.運作結果
(3)模拟實作strcmp
a.代碼
#include<stdio.h>
#include<string.h>
int MyStrCmp(const char * str1, const char * str2)
{
//逐個元素比較
while (*str1 && *str2)
{
if (*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
else //如果兩個元素相等,進入下一個繼續比較
{
++str1;
++str2;
}
}
//str2比較完了,str1還有
if (*str1)
{
return 1;
}
//str1比較完了,str2還有
else if (*str2)
{
return -1;
}
//str1等于str2
else
{
return 0;
}
}
int main()
{
char* p1 = "abcd";
char* p2 = "abcd";
char* p3 = "abcde";
char* p4 = "bcd";
char* p5 = "b";
printf("%d\n", MyStrCmp(p1,p2 ));
printf("%d\n", MyStrCmp(p1,p3 ));
printf("%d\n", MyStrCmp(p1,p4 ));
printf("%d\n", MyStrCmp(p4,p5 ));
return 0;
}