字符串處理函數

字符串處理
1,求字符串長度
size_t strlen(char const *string);
size_t:無符號整數類型

char buffer[]="hello world";
printf("%d\n",strlen(buffer));

2,複製字符串
char * strcpy(char * dst,char const *src);
這個函數把參數src字符串複製到dst字符串
必須保證:目標字符數組的空間足以容納需要複製的字符串。

#include<stdio.h>
#include<string.h>
int main()
{
    char message[100]="hello world";
    strcpy(message,"yang kai");
    printf("%s",message);
    return 0;
}

3,連接字符串
char * strcat(char * dst,char const *src;
src中的字符,被放到了dst最後的字符的

char message[100]="hello world";
strcat(message," yang kai");
/*
函數strcpy(),和函數strcat()的函數返回值都是char* 是第一個參數的一份拷貝,
就是一個指向目標字符數組的指針
*/

4,字符串比較函數
int strcmp(char const *s1,char const *s2);

發佈了21 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章