c语言中字符串与字符串数组详解

在C语言当中,字符串数组可以使用char a[] [10]; 或者char *a[]; 表示,下面这篇文章主要给大家介绍了关于c语言中字符串与字符串数组的相关资料,需要的朋友可以参考下

字符串

  • 用双引号引起来的就是字符串,字符串由字符组成
  • 字符串使用%s格式化输出
  • 字符串以\0结尾,没有\0就不是字符串
  • 只要是用双引号括起来的都是字符串
  • 字符串的本质就是数组
    注意: 字符串变量和普通的字符数组有一定的区别,C语言规定,字符串必须以\0结尾(作为字符串的结束符号),所以字符串变量的元素个数比字符数组的元素多一个\0
 #include  int main(int argc, const char * argv[]) { char name[] = "zhangsan"; // %u 表示无符号,sizeof 返回的是一个无符号长整型 printf("name --> %s  size --> %lu\n", name, sizeof(name));      // name --> zhangsan  size --> 9 // 字符串本质就是一个字符数组,前提是末尾以\0结尾 char name1[] = {'z', 'h', 'a', 'n', 'g', 's', 'a', 'n', '\0'}; printf("name1 --> %s\n", name1);    // name1 --> zhangsan // 部分初始化中,没有被初始化的元素默认是0, \0 对应的ASCII值是 0 char name2[9] = {'z', 'h', 'a', 'n', 'g', 's', 'a', 'n'}; printf("name2 --> %s\n", name2);    // 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函数进行输出
  - 优点:可以自动换行
  - 缺点:不可以自定义格式,只能原样输出

 // printf char str[] = “lisi”; printf(“name = %s!!!\n”, str); printf("-----\n"); // puts // 自动换行,只能原样输出 puts(str); printf("----\n"); 

#### 字符串输入

字符串输入可以使用 scanf 和 gets 进行输入,各有利弊

输入字符串

  • 使用scanf的%s占位符接收键盘输入的字符串
    • scanf 接收字符串,会以空格,tab,回车作为结束符号,利用scanf接收字符串时,字符串不能出现空格,tab,回车
  • 使用gets接收字符串
 // scanf printf("请输入一个字符串:\n"); char buf[10]; scanf("%s", buf);   // 输入:fdsa fdas printf("buf --> %s\n", buf);    // buf --> fdsa // gets char buf[10]; printf("请输入一个字符串,使用gets接收:\n"); gets(buf); printf("buf --> %s\n", buf);    // buf --> fdsaf fdsa 

字符串常用方法

计算字符串的长度

 #include  #include  // 申明函数 int stringLen(char value[]); int main(int argc, const char * argv[]) { char string[] = "zhangsan"; // 计算字符串的长度,不包括末尾\0的长度 // 调用系统内置函数strlen 计算字符串的长度,需要先导入 string.h 头文件 size_t length = strlen(string);     // lenght --> 8 printf("lenght --> %lu\n", length); // 自定义计算字符串长度的函数 int length2 = stringLen(string); printf("length2 --> %i\n", length2);    // length2 --> 8 return 0; } // 自定义计算字符串长度的函数 int stringLen(char value[]) { int count = 0; while (value[count] != '\0') { count ++; } return count; } 

字符串拼接

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

使用内置方法strncat进行2个字符串拼接,可以指定将后者的前多少个字符与前者进行拼接

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

字符串拷贝

strcpy 函数会将源的数据拷贝到目录中,并且会覆盖掉目标中原有的数据,目标的长度必须能够存放拷贝的数据,长度不够会报错

strncpy 函数在strcpy函数的基础之上,增加一个参数,可以指定拷贝几个字符0

 // 将str2中的内容拷贝到str1中 char str1[20] = "hello"; char str2[] = " world!"; printf("str1 拷贝前 --> %s\n", str1);      // str1 --> hello printf("str2 拷贝前 --> %s\n", str2);      // str2 -->  world! strcpy(str1, str2); printf("str1 拷贝后 --> %s\n", str1);      // str1 -->  world! printf("str2 拷贝后 --> %s\n", str2);      // str2 -->  world! // strncpy 函数在strcpy函数的基础之上,增加一个参数,可以指定拷贝几个字符 char str3[20] = "hello"; char str4[] = "_world!"; strncpy(str3, str4, 2); printf("str3 拷贝后 --> %s\n", str3);      // str3 拷贝后 --> _wllo printf("str4 拷贝后 --> %s\n", 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 --> %i\n", result);  // result --> 0 char str1[] = "ab1"; char str2[] = "abc"; int result = strcmp(str1, str2); printf("result --> %i\n", result);  // result --> -50 char str1[] = "absadsa"; char str2[] = "abc"; int result = strcmp(str1, str2); printf("result --> %i\n", result);  // result --> 16 

字符串数组

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

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

总结

到此这篇关于c语言中字符串与字符串数组的文章就介绍到这了,更多相关c语言字符串与字符串数组内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是c语言中字符串与字符串数组详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言