C语言基础——字符串操作

一、字符串的常用定义

    char str[5] = {'a','b','c','d','e'};//第一种方式,最傻方式
    char str2[6] = "abcde";//第二种方式
    char str3[] = "abcdefefsdfg";//第三种方式,数组元素个数不写,会根据真实大小来默认分配

    char *pstr = "hello world !";
    printf("%s\n",pstr);//字符串用格式占位符%s表示,不需要用i的下标遍历

二、字符串的内存存放方式及结束标志

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    int data[] = {1,2,3,4,5};
    char stu[5] = {'h','e','l','l','o'};
    char stu2[] = "hello";
    int len;

    len = sizeof(data)/sizeof(data[0]);
    printf("len = %d\n",len);

    len = sizeof(stu)/sizeof(stu[0]);
    printf("len = %d\n",len);

    len = sizeof(stu2)/sizeof(stu2[0]);
    printf("len = %d\n",len);
    return 0;
}
len = 5
len = 5
len = 6

三、sizeof和strlen的区别

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(){}
int main(){

    char cdata[] = "hello";//'\0'

    printf("sizeof:%d\n",sizeof(cdata));
    printf("strlen:%d\n",strlen(cdata));

    void (*ptest)();
    ptest = test;
    char *p = "hello";
    //p是一个char *,sizoef来计算的时候,得出是计算机用多少字节来表示一个地址
    printf("sizeof:p       :%d\n",sizeof(p));
    printf("sizeof:char *  :%d\n",sizeof(char *));
    printf("sizeof:int *   :%d\n",sizeof(int *));
    printf("sizeof:char    :%d\n",sizeof(char));
    printf("sizeof:ptest   :%d\n",sizeof(ptest));
    printf("strlen         :%d\n",strlen(p));
    return 0;
}
sizeof:6
strlen:5
sizeof:p       :8
sizeof:char *  :8
sizeof:int *   :8
sizeof:char    :1
sizeof:ptest   :8
strlen         :5

四、字符串的常用函数

1.puts(s);输出字符串

    char *str="hello world";
    puts(str);//printf("%s\n",str);

2.get(s);获取字符串

#include <stdio.h>
#include <stdlib.h>    //malloc所需要包含的头文件
#include <string.h>    //memset所需要包含的头文件

int main(){

    char *str;
    str = (char *)malloc(128);//申请128字节的空间
    memset(str,'\0',128);//1.初始化对象开头 2.初始化成什么字符 3.多大
    gets(str);//scanf("%s",str);
    puts(str);
    return 0;
}
hello world
hello world

3.strlen(s);计算字符串的有效长度

strlen(str);

4.字符串的拷贝strcpy和strncpy

原函数声明

char *strcpy(char* dest, const char *src);
char *strncpy(char *destinin, char *source, int maxlen);

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char *strSrc = "hello world!";
    char strDest[128] = {'\0'};

    strcpy(strDest,strSrc);
    puts(strDest);

    memset(strDest,'\0',sizeof(strDest)/sizeof(strDest[0]));
    strncpy(strDest,strSrc,3);
    puts(strDest);
    return 0;
}

运行结果

hello world!
hel

5.字符串的拼接strcat

函数原型

char *strcat(char *dest, const char *src);

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char str[128] = "hello ";
    char *Src = "world";

    strcat(str,Src);
    puts(str);
    return 0;
}
hello world

6.字符串的比较strcmp

两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。

函数原型

int strcmp(const char *s1,const char *s2);

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char *str1 = "123";
    char *str2 = "1234";

    int ret = strcmp(str1,str2);
    int ret2 = strcmp(str2,str1);

    printf("ret = %d\n",ret);
    printf("ret2 = %d\n",ret2);

    if(strcmp(str1,str2) == 0){
        printf("两个字符串一样\n");
    }

    return 0;
}

运行结果

ret = -1
ret2 = 1

7.查找子字符strchr

函数原型

char *strchr(const char *str, int c)

参数

  • str-- 要被检索的 C 字符串。

  • c-- 在 str 中要搜索的字符。

功能

在参数str所指向的字符串中搜索第一次出现字符c(一个无符号字符)的位置。

返回值

返回一个指向该字符串中第一次出现的字符的指针,如果字符串中不包含该字符则返回NULL空指针。

头文件

#include <string.h>

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char *str = "hello world!";
    char c = 'o';
    char *p = NULL;
    p = strchr(str,c);
    if(p == NULL){
        printf("没有找到\n");
    }else{
        puts(p);
    }

    return 0;
}

运行结果

o world!

8.查找子串strstr

函数原型

char *strstr(char *str1, const char *str2);

语法

strstr(str1,str2);

str1: 被查找目标 string expression to search.

str2: 要查找对象 The string expression to find.

返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char *str1 = "hello world!";
    char *str2 = "lo";
    char *p = NULL;
    p = strstr(str1,str2);
    if(p == NULL){
        printf("没有找到\n");
    }else{
        puts(p);
    }

    return 0;
}

运行结果

lo world!

9.转为小写字母的函数strlwr和转为大写字母的函数strupr

函数原型

char *strlwr(char *s);
char *strupr(char *s);

头文件

#include <string.h>

功能:将字符串s参数转换为小写(大写)形式

说明:只转换s参数中出现的大写(小写)字母,不改变其它字符。返回指向s参数的指针

返回值:返回转换后的小写(大写)字符串,其实就是将 str 返回

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    char str[] = "Hello World";//windows不能用char *str = "Hello World";而Linux下可以
    puts(strlwr(str));
    puts(strupr(str));
    return 0;
}

运行结果

hello world
HELLO WORLD

10.字符串的分割函数strtok

函数原型

char *strtok(char s[], const char *delim);

第一个参数:要分割的字符串          第二个参数:要分割的字符类型

用例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){

    int i = 1;
    char str[] = "hello1,hello2,hello3,hello4,hello5";
    char *p;
    p = strtok(str,",");
    printf("获取到第 %d 个串:p = %s\n",i,p);

    while(1){
        i++;
        p = strtok(NULL,",");//获取第二个串开始,第一个参数都要写 NULL
        if(p != NULL){
            printf("获取到第 %d 个串:p = %s\n",i,p);
        }else{
            printf("没有串了\n");
            break;
        }
    }
    return 0;
}

运行结果

获取到第 1 个串:p = hello1
获取到第 2 个串:p = hello2
获取到第 3 个串:p = hello3
获取到第 4 个串:p = hello4
获取到第 5 个串:p = hello5
没有串了

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从入门到捕蛇者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值