• 字符串拼接函数

strcat()

  • 字符串----作为很多编程语言的鼻祖C语言,没有字符串类型,取而代之的字符数组,很多数组在声明的时候会给定数组长度,然而我们却可以这样写

char mywords[] = "you and me hold breath count to 3";

  • 计算字符串(实际上是字符数组)长度 strlen()
#include <stdio.h>
#include <string.h>
void main()
{
   printf("喜欢陈培昌");
   getchar();
   char mywords[] = "you and me hold breath count to 3";
   printf(strcat(mywords,"\n"));
   printf("字符数组的长度是%d",strlen(mywords));
   printf("\n");
}

输出结果:

  • 遍历输出数组元素

有其他语言编程习惯的朋友,习惯在循环条件中声明循环变量i的类型,but 在C语言,I'm deeply sorry这就出错了

c语言的习惯是先声明循环变量,然后再循环条件中使用定义好的循环变量

#include <stdio.h>
#include <string.h>
void main()
{
char mywords[] = "劲儿弟弟和昌仔在擂台实战,汗水,昏黄的灯光,青年搏击运动员荷尔蒙,鞋柜里散发的脚味,姑娘们和老外的香水味,混杂在一起,越发的让一切迷乱起来";
int i=;
for(i=;i<strlen(mywords);i++)
{
printf("%c",mywords[i]);
}
printf("\n");
}
  • 数组元素逆序
#include <stdio.h>
#include <string.h>
void main()
{
char mywords[] = {"don't break myheart"};
char reversearray[sizeof(mywords)] = {};
int arraysize;
arraysize = sizeof(mywords);
printf("%d",arraysize);
printf("\n");
int i=;
for(i=;i<arraysize;i++)
{
reversearray[arraysize-i-]=mywords[i];
}
printf(reversearray);
printf("\n");
}

注意,字符数组变量值的部分一定要用{}包裹,声明的空数组一定要用{0}包裹,否则将无法得到实验结果

中文字符串的处理需要另外讨论

输出结果:

  • 宽字符----可以处理中文的类型

注意,下列上机环境为centos7,windows下写法略有不同,主要体现在本地化函数setlocate上面,如果你参照的出版物无法得到预期结果,可能和操作系统有关

#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
void main()
{
setlocale(LC_ALL, "zh_CN.UTF-8");
wchar_t mywords = L'冬'; #注意,单一字符不用加方括号[]
   wprintf(L"%lc \n",mywords);#单一字符输出格式为lc
wchar_t iwant[] = L"想用脸碰一下劲儿弟弟的毛儿扎头";
wprintf(L"%ls \n",iwant);#字符串输出格式为ls
}

输出结果:

最新文章

  1. 【转】深入分析 iBATIS 框架之系统架构与映射原理
  2. ubuntu下查看文件md5
  3. radioButton的简单使用
  4. Qt之XML(一) DOM
  5. 十个iOS面试问题
  6. JS精粹(二)
  7. Qt on Android: Qt 5.3.0 公布,针对 Android 改进的说明
  8. 使用筛选器特性标记方法解决webapi 跨域问题
  9. Java面向对象抽象类案例分析
  10. httpclient案例一(调用识别接口)
  11. Python用可变参数找出最大值和最小值
  12. P2495 [SDOI2011]消耗战 lca倍增+虚树+树形dp
  13. 洛谷P1119 灾后重建 Floyd + 离线
  14. 蓝图Tips
  15. UILabel部分文字可点击
  16. 基于vue.js实现远程请求json的select控件
  17. 利用gulp搭建less编译环境
  18. JAVA中销毁session的代码
  19. numpy 傅立叶得到幅值和频率
  20. Vue-cli / webpack 加载静态js文件的方法

热门文章

  1. 本机sshd服务开启但是其他机器不能登陆
  2. curl使用举例
  3. 【AtCoder】ARC059
  4. 【转载】SpringBoot-配置发送邮件遇到的一些问题
  5. LC 202. Happy Number
  6. selenium弹框元素定位-冻结界面
  7. resful规范: 进行数据交换时的代码潜规则
  8. iframe/frameset/frame的区别
  9. 【Trie】Phone List
  10. QT Creator 使用SVN的版本号做为编译的版本信息