您的位置:首页 > 编程语言 > C语言/C++

C语言 字符串处理函数

2017-05-24 16:15 169 查看
#include <stdio.h>
#include <string.h>

// strlen
void test() {
// 测量字符串常量的字符长度(不包括\0这个字符)
int len =  strlen("李某某");
//printf("%d\n", len);

// 测量字符串变量的字符长度
char s[] = "lmj";
//printf("%d\n", strlen(s));

char s1[] = {'m', 'j'};

// 结果是5 内存中的存储是:mjlmj\0
printf("%d\n", strlen(s1));
}

// strcpy
void test1() {
char left[10];

// 拷贝"itcast"到数组left中
strcpy(left, "itcast");

printf("%s", left);
}

// strcat
void test2() {
char left[10] = {'m', 'j', '\0', 'i', 't', 'c', 'a', '\0'};
// 从left的第一个\0开始拼接ios这个字符串
// 拼接的结果是:mjios\0
strcat(left, "ios");
// 拼接的结果:mjios\0a\0

printf("%s", left);
}

// strcmp
void test3() {
int delta = strcmp("abc", "aac");
printf("%d", delta);
}

int main(int argc, const char * argv[])
{
test3();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: