首页 > 编程知识 正文

typedef struct 用法(字符串和数组的区别和联系)

时间:2023-05-06 13:38:12 阅读:76078 作者:322

文章目录字符串输出字符串常用方法计算字符串长度的字符串拼接字符串复制字符串比较字符串数组

字符串用双引号括起来就是字符串。 字符串由字符组成的字符串使用%s格式,输出字符串以结尾。 否则不是字符串。 如果用双引号括起来,字符串的本质与数组注意:字符串变量在普通的字符数组中是不同的,在c语言中,字符串以0作为字符串的结束符号结束,因此字符串变量的元素数比字符数组的元素数多1个# include //%u表示无符号,sizeof返回无符号的长整数printf。 (' name----%ssize----%Lun ',name,sizeof(name ) ) ) ) ) ) )。 //name----Zhangsansize----9//字符串的本质是末尾以0结尾的char name1[]={'z '、' a '、' n '、' g '、' s '和' //name1 -- zhangsan //部分初始化中,未初始化的元素默认为0,对应的ASCII值为0 char name2[9]={'z '、' h '、' a '、' n '和' //name2 -- zhangsan //字符串的本质是数组char name3[]='lisi '; printf(name3----%s(n )、name3); //name3 -- lisi name3[0]='x '; printf(name3--%s(n ),name3); //name3 -- xisi return 0; }字符串输出

字符串输出可以使用printf和puts输出,各有利弊

-使用输出字符串printf中的%s占位符输出-弊端:如果想换行,必须加上n优点:可以自定义格式化所需的字符串,也就是说,可以使用可以以我们所需格式输出的puts函数输出-优点

char str[]=“lisi”;

printf(「name=%s! n”,str;

printf(----(n ) );

//puts

//自动换行只能直接输出

压力(str;

printf(----(n ) );

####字符串输入字符串输入可以使用scanf和gets输入。 各有利弊。 输入字符串使用scanf中的%s占位符接收通过键盘输入的字符串scanf并接收字符串。 以空间、制表符和返回为终结符。 使用scanf接收字符串时,字符串中不能有空格。 tab,返回使用gets接收字符串//scanfpror char buf [ 10 ]; 扫描(' % s ',buf ); //输入: fdsaFDAsprintf(buf--%sn )、buf ); //buf -- fdsa//getschar buf[10]; 请输入printf ('字符串。 通过gets接收。 n ); gets(buf ); printf(buf--%s(n ),buf ); //buf -- fdsaf fdsa字符串常用方法字符串长度# include stdio.h # include string.h//声明函数intstringlen(charvalue[] ); intmain(intargc,const char * argv[] () charstring ) )='Zhangsan '; //除末尾0的长度之外的计算字符串的长度//要调用系统内置函数strlen来计算字符串的长度,必须首先读取string.h头文件size_tlength=strlen(string )。 //lenght----8printf (lenght---% Lu (n ),length ); //计算字符串长度的函数intlength2=stringlen(string ); printf(length2--%I(n ),length2);//长度2--

8 return 0;}// 自定义计算字符串长度的函数int stringLen(char value[]) { int count = 0; while (value[count] != '') { count ++; } return count;} 字符串拼接

使用内置方法strcat进行2个字符串拼接,将后者拼接到前者后面,前提是前者空余的长度要大于后者的长度,否则会报错
使用内置方法strncat进行2个字符串拼接,可以指定将后者的前多少个字符与前者进行拼接

// 字符串拼接char str1[20] = "hello";char str2[10] = " world";printf("str1拼接前 --> %sn", str1); // str1拼接前 --> helloprintf("str2拼接前 --> %sn", str2); // str2拼接前 --> worldstrcat(str1, str2);printf("n");printf("str1拼接后 --> %sn", str1); // str1拼接后 --> hello worldprintf("str2拼接后 --> %sn", str2); // str2拼接后 --> world// 使用 strncat 可以指定将后者的前多少个字符与前者进行拼接strncat(str1, str2, 3);// 最后一个参数3表示指定str2的前多少个字符拼接到str1后面printf("str1拼接后 --> %sn", str1); // str1拼接后 --> hello world woprintf("str2拼接后 --> %sn", str2); // str2拼接后 --> world 字符串拷贝

strcpy 函数会将源的数据拷贝到目录中,并且会覆盖掉目标中原有的数据,目标的长度必须能够存放拷贝的数据,长度不够会报错
strncpy 函数在strcpy函数的基础之上,增加一个参数,可以指定拷贝几个字符0

// 将str2中的内容拷贝到str1中char str1[20] = "hello";char str2[] = " world!";printf("str1 拷贝前 --> %sn", str1); // str1 --> helloprintf("str2 拷贝前 --> %sn", str2); // str2 --> world!strcpy(str1, str2);printf("str1 拷贝后 --> %sn", str1); // str1 --> world!printf("str2 拷贝后 --> %sn", str2); // str2 --> world!// strncpy 函数在strcpy函数的基础之上,增加一个参数,可以指定拷贝几个字符char str3[20] = "hello";char str4[] = "_world!";strncpy(str3, str4, 2);printf("str3 拷贝后 --> %sn", str3); // str3 拷贝后 --> _wlloprintf("str4 拷贝后 --> %sn", str4); // str4 拷贝后 --> _world! 字符串比较

strcmp 会对传入的字符串进行比较,比较完毕后会返回一个整型的值

/*int result = strcmp(str1, str2)如果result等于0,证明两个字符串相等如果result小于0,证明str1小于str2如果result大于0,证明str1大于str2*/char str1[] = "abc";char str2[] = "abc";int result = strcmp(str1, str2);printf("result --> %in", result); // result --> 0char str1[] = "ab1";char str2[] = "abc";int result = strcmp(str1, str2);printf("result --> %in", result); // result --> -50char str1[] = "absadsa";char str2[] = "abc";int result = strcmp(str1, str2);printf("result --> %in", result); // result --> 16 字符串数组

如果想存储一堆字符串可以使用字符串数组,字符串数组就是二维数组

char names[5][20] = {"zhangsan","lisi","wangwu",}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。