粗暴,干就完了----徐晓冬似的C语言自学笔记---字符数组相关技术
2024-09-07 10:36:33
- 字符串拼接函数
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
}
输出结果:
最新文章
- 【转】深入分析 iBATIS 框架之系统架构与映射原理
- ubuntu下查看文件md5
- radioButton的简单使用
- Qt之XML(一) DOM
- 十个iOS面试问题
- JS精粹(二)
- Qt on Android: Qt 5.3.0 公布,针对 Android 改进的说明
- 使用筛选器特性标记方法解决webapi 跨域问题
- Java面向对象抽象类案例分析
- httpclient案例一(调用识别接口)
- Python用可变参数找出最大值和最小值
- P2495 [SDOI2011]消耗战 lca倍增+虚树+树形dp
- 洛谷P1119 灾后重建 Floyd + 离线
- 蓝图Tips
- UILabel部分文字可点击
- 基于vue.js实现远程请求json的select控件
- 利用gulp搭建less编译环境
- JAVA中销毁session的代码
- numpy 傅立叶得到幅值和频率
- Vue-cli / webpack 加载静态js文件的方法