C语言-字符串

字符串基础

c语言没有专门用于存储字符串的变量类型,字符串都被存储在char类型的数组中。数组由连续的存储单元组成,字符串中的字符被存储在相邻的存储单元中,每个单元存储一个字符,c语言用数组末尾的字符\0标记字符串的结束。
注意:字符串一定是一个char型的数组,但char型数组未必是字符串

在这里插入图片描述

字符串基础使用

对于字符常量的注意

这些字符串常量是不能改变的,如果试图改变指针所指向的内容是错误的
因为字符串常量是存在静态内存区的,不可以改变的。
如定义字符串常量:
char* a=“i love you.”;
a[3]=‘p’(非法操作)。
因为a[3]本来就是一个常量,常量不能被改变。(比如 1=2一样)
如定义字符串变量:
char a[]=“i love you.”;
a[3]=‘p’是可以的.
因为这个a[3]不同于上例中的 这个是一个 char 类型的变量 。

常见第一种基本用法

在c语言中,字符使用单引号,而字符串使用双引号。

#include<stdio.h>
int main()
{	
	char string1[] = "hello" "world" "!";
	char string2[] = {'h','e','l','l','o','w','o','r','l','d',0};
	printf("string1:%s\n",string1);
	printf("string2:%s\n",string2);
	printf("string1的长度为:%ld\n",sizeof(string1));
	printf("string2的长度为:%ld\n",sizeof(string2));
	return 0;
}

输出结果:
在这里插入图片描述

常见第二种基本用法

为了存储多个字符串,我们可以使用字符串指针数组,例如:

#include<stdio.h>
int main()
{	
	char *str[] = {"hello","happy","good"};
	printf("第一个字符串:%s\n",str[0]);
	printf("第二个字符串:%s\n",str[1]);
	printf("第三个字符串:%s\n",str[2]);
	printf("第二个字符串的第二个字符:%c",*(*(str+1)+1));
	return 0;
}

在这里插入图片描述

字符串常见函数

均来自#include<string.h>

求字符串的长度

补充:long是确定的占4个字节
这里还有一个类似的运算符sizeof(),因为sizeof是求地址所以计算长度的时候会加上’\0’

#include<stdio.h>
#include<string.h>
int main()
{	
	char str[]="hello,world";
	char str1[]={'h','e','l','\0','o'};
	printf("字符串长度为:%ld\n",strlen(str));
	printf("字符串的长度:%ld\n",sizeof(str));
	printf("字符串1的长度:%ld\n",sizeof(str1));
	printf("字符串长度为:%ld\n",strlen(str1));
	printf("字符串1:%s\n",str1);
	return 0;
}

在这里插入图片描述
分析这个例子的结果我们可以看到:strlen和printf以字符串输出都是检测的\0
,检测到\0后后面的数的忽略掉,但是strlen输出的长度并没有包括\0

复制、拼接、比较(长度不受限制)

(1)strcpy(字符串复制函数,将source字符串复制到destination。)
char * strcpy(char * destination,const char * source);
●源字符串必须以 '\0’结束。
●会将源字符串中的 '\0’拷贝到目标空间。
●目标空间必须足够大,以确保能存放源字符串。

#include<stdio.h>
#include<string.h>
int main()
{	
	char str[]="hello,world";
	char str1[50]={};
	strcpy(str1,str);
	printf("%s",str1);
	return 0;
}
//输出结果 
//hello,world

(2)strcat(字符串拼接函数,本质也是拷贝,将source字符串拼接到destination字符串。)
char * strcat ( char * destination, const char * source );
●源字符串必须以 '\0’结束。
●目标空间必须有足够的大,能容纳下源字符串的内容。
●目标空间必须可修改。

#include<stdio.h>
#include<string.h>
int main()
{	
	char str[50]="hello";
	char str1[]="world";
	strcat(str,str1);
	printf("%s",str);
	return 0;
}
//输出结果 
//helloworld

(3)strcmp(字符串比较函数,通常是比较字符大小,与字符串长度无关)
int strcmp ( const char * str1, const char * str2 );
●第一个字符串大于第二个字符串,则返回大于0的数字。
●第一个字符串等于第二个字符串,则返回0。
●第一个字符串小于第二个字符串,则返回小于0的数字。
注意:这里的比较指的是从第一个字符比较到最后一个字符,比较的是ascii码。

#include<stdio.h>
#include<string.h>
int main()
{	
	char str[50]="h";
	char str1[]="w";
	printf("%d",strcmp(str,str1));
	return 0;
}
//输出结果 
//-15

复制、拼接、比较(长度受限)

1)strncpy
char * strncpy ( char * destination, const char * source, size_t num );
●拷贝num个字符从源字符串到目标空间。
●如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
(2)strncat
char * strncat ( char * destination, const char * source, size_t num );
●将源的第一个数字字符附加到目标,再加上一个终止的空字符。
●如果source中C字符串的长度小于num,则只复制不超过终止空字符的内容。
(3)strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
●比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

字符查找、分割

(1)strstr(在长字符串里面找第一次自字符串,找到的话,就输出从找到位置开始至到长字符串结束的字符串。找不到就输出NULL。)
char * strstr ( const char * , const char * );
●返回指向长字符串中第一次出现自字符串的指针,如果子字符串不是长字符串的一部分,则返回空指针。
●它用于找到子串(str2)在一个字符串(str1)中第一次出现的位置。

#include<stdio.h>
#include<string.h>
int main()
{	
	char str[50]="hhldsfjklsdjfhelloldlfjlsdjflkjl";
	char str1[]="hello";
	printf("%s",strstr(str,str1));
	return 0;
}
//输出结果 
//helloldlfjlsdjflkjl

(2) char * strtok ( char * str, const char * sep );
●sep参数是个字符串,定义了用作分隔符的字符集合。
●第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
●strtok函数找到str中的下一个标记,并将其用 \0结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
●strtok函数的第一个参数不为 NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
●strtok函数的第一个参数为 NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
●如果字符串中不存在更多的标记,则返回 NULL指针。

具体使用方法:
在使用 strtok 函数时,第一次调用需要给定 str 参数的值,往后的每一次调用只需将 str 参数设置为 NULL 即可。我们通常使用 while 循环自动分割字符串的所有字符
第一个参数不是 NULL 时,strtok 函数查找 str 下一个标记,以 \0 结尾,strtok 函数会保存标记的地址。
第一个参数为 NULl 时,strtok 函数取上次分割字符串后剩余的字符串继续进行分解操作,如果没有更多的标记则返回 NULL。

#include <stdlib.h>
#include "stdio.h"
#include "string.h"
int main() {
        char *temp = NULL;
        char input[128] = "#10?-ssid=test&-passwd=12345678&-netmask=255.255.255.0&-gateway=192.168.1.1";
        //char *input = (char *)malloc(128); 
        //scanf("%s",input);
        printf("%s\n",input); 
        temp = strtok(input,"?&");
        printf("input=%s,temp=%s\n", input, temp);

        while(temp != NULL){
                temp = strtok(NULL,"?&");
                printf("input=%s,temp=%s\n", input, temp);
        };
        return 0;
}

#10?-ssid=test&-passwd=12345678&-netmask=255.255.255.0&-gateway=192.168.1.1
input=#10,temp=#10
input=#10,temp=-ssid=test
input=#10,temp=-passwd=12345678
input=#10,temp=-netmask=255.255.255.0
input=#10,temp=-gateway=192.168.1.1
input=#10,temp=(null)

(3) strchr()
char *strchr(const char *s, int c);
在字符串 s 中查找字母 c 出现的位置

错误信息报告

strerror()函数用于:获取指向错误消息字符串的指针
函数声明: char * strerror ( int errnum );
头 文 件:#include <string.h>
返 回 值: 返回值为char * 类型 。指向描述错误错误的错误字符串的指针。

在这里插入图片描述

内存操作

(1)memcpy(拷贝函数,与stccpy的区别,strcpy只能拷贝字符串,而memcpy却能拷贝结构体、字符串、数组等。)
void * memcpy ( void * destination, const void * source, size_t num );
●函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
●这个函数在遇到 '\0’的时候并不会停下来。
●如果source和destination有任何的重叠,复制的结果都是未定义的。
(2)memmove(其实也是拷贝函数,只不过可以进行重叠内存部分的拷贝。)
void * memmove ( void * destination, const void * source, size_t num );
●和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
●如果源空间和目标空间出现重叠,就得使用memmove函数处理。
(3)memset
void * memset ( void * ptr, int value, size_t num );
●将ptr中的num个字节设置成value。
●注意:memset是对一个字节进行操作的,所以如果是对char类型进行操作的话,完全没有问题,因为char类型本身就是占一个字节。但是对其余类型的变量进行操作,只有赋值0的时候,才是正确的,否则就会出现其他的值。
(4)memcmp
int memcmp ( const void * ptr1,const void * ptr2, size_t num );
●比较从ptr1和ptr2指针开始的num个字节
●当ptr1大于ptr2时,输出大于0的数。当ptr1小于ptr2时,输出小于0的数。两者相等时,输出零。
●注意:这里也是比较的对应字符的ascii码。

字母大小转化、格式化

strlwr :将字符串中的大写字母转换成小写字母
使用方法:srtlwr(str)
会直接转化其中的字母

strupr:小写字母转化成大写字母,使用方法同理

分析一下sprintf()指的是字符串格式化命令
当需要在控制台打印字符串的时候,会用到printf函数,printf会将格式化后的字符串打印在控制台上,如果需要将数字或者其他数据格式化成字符串保存到一个字符串变量中,会调用另一个函数sprintf。

#include <stdio.h>
#include <string.h>
int main()
{
	int i = 0;
	char string[5] = { 'I','L','O','V','E'};
	/*  Write C code in this online editor and run it. */
	sprintf(string, "H%s","I");
	for (i = 0; i < 5; i++)
	{
		printf("string[%d]=%c\n",i,string[i]);
	}
	return 0;
}

比如这个例子

string[0]=H
string[1]=I
string[2]=
string[3]=V
string[4]=E

我们分析这个结果由于新的格式化的字符串为HI\0 所以第三个会为0,但是由于小于原来的string,后面的值也并没有改变,如果真的需要这样,需要提前使用memset函数清理内存

其他例子如我们格式化整数:
sprintf(string, “%d”,123);
把整数123格式化成字符串存在sting中

格式化浮点数:
sprintf(string, “%f”,3.1415626);//3.141593
字符串连接:

#include <stdio.h>
#include <string.h>

int main()
{
	int i = 0;
	char string[17];//注意加上`\0`

	char* name = "Tom";
	char* base = "Asia";

	/*  Write C code in this online editor and run it. */
	sprintf(string,"%s live in %s",name,base);
	printf("%s", string);

	return 0;
}
> 结果:Tom live in Asia

字符分类函数

在这里插入图片描述

字符串转数字

(1) 字符串转整型 atoi()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    char str[10] = "100";
    int value;
    value = atoi(str);
    printf("%d\n",value);
    return 0;
}

(2) 字符串转浮点数 atof()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    char str[10] = "100";
    double value;
    value = atof(str);
    printf("%lf\n",value);
    return 0;
}

(3)字符串转整型 strtol()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值