目錄:
- 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;
}
b.運行結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/249004.html
標籤:其他
