字符串和字符串数组

字符串和字符串数组文章目录字符串字符串输出字符串常用方法计算字符串的长度字符串拼接字符串拷贝字符串比较字符串数组字符串用双引号引起来的就是字符串 字符串由字符组成字符串使用 s 格式化输出字符串以 0 结尾 没有 0 就不是字符串只要是用双引号括起来的都是字符串字符串的本质就是数组注意 字符串变量和普通的字符数组有一定的区别 C 语言规定 字符串必须以 0 结尾 作为字符串的结束符号 所以字符串变量的元素

字符串

  • 用双引号引起来的就是字符串,字符串由字符组成
  • 字符串使用%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函数进行输出 - 优点:可以自动换行 - 缺点:不可以自定义格式,只能原样输出 
 # 字符串输入 字符串输入可以使用 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; } 
    
  
字符串拼接
// 字符串拼接 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 
字符串拷贝
// 将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", } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201295.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午9:39
下一篇 2026年3月20日 上午9:39


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号