C语言初级-字符串处理函数

//

//  main.c

//  test

// Visual Studio 2010

//  Created by AirDebug on 2020/2/15.

//  Copyright @ 2020 AirDebug. All rights reserved.

//

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main(int argc, const char * argv[])

{

//strcat(char str1,char str2)

char str1[20]={"Hello "};//str1长度必须长于两个字符串相加长度否则内存溢出如:char str1[8]={"Hello "};

char str2[]={"World!"};

printf("strcat(char str1,char str2):%s\n",strcat(str1,str2));//将str2连接到str1后面,将str1尾部\0取消,只在最后保留\0

 

//strcpy(char str1,char str2)

char str3[20]={"Hello "};

char str4[]={"World!"};

printf("strcpy(char str1,char str2):%s\n",strcpy(str3,str4));//将str2拷贝到str1

 

//strncpy(char str1,char str2,int n)

char str5[20]={"Hello "};//str1长度必须长于两个字符串相加长度否则内存溢出如:char str1[8]={"Hello "};

char str6[]={"World!"};

printf("strncpy(char str1,char str2,int n):%s\n",strncpy(str5,str6,2));//将str2前n个字符拷贝到str1前n个字符*/

 

//strcmp(char str1,char str2)//比较两个字符串大小str1>str2返回值>0,str1=str2返回值=0,str1<str2返回值<0

char str7[5]={"ABC"};

char str8[5]={"BAC"};

char str9[5]={"BBC"};

if(strcmp(str7,str8)<0)//比较两个字符串大小str1>str2返回值>0,str1=str2返回值=0,str1<str2返回值<0

printf("ABC<BAC\n");

if(strcmp(str8,str7)>0)

printf("BAC>ABC\n");

if(strcmp(str7,"ABC")==0)

printf("ABC=ABC\n");

 

//strlen(char str)返回字符串数据长度

char str10[20]= "Hello World!";

printf("char str10[20]=\"Hello World!\"数据长度:%d,sizeof(str10)空间长度:%d\n",strlen(str10),sizeof(str10));

 

//strlwr(char str)转换为小写

//strupr(char str)转换为大写

char str11[10]="AbCd";

printf("\"AbCd\"转换为小写%s\n",strlwr(str11));

printf("\"AbCd\"转换为大写%s\n",strupr(str11));

 

system("pause");//解决Visual Studio一闪而过,需要<stdlib.h>头文件

    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值