c字符数组之两头堵模型
- char *其实就是char[length]的首元素地址 实验环境:centos7下qt5.11 中文char类型占3个字节
char[length]="特别车队"其实等价于char *mywords="特别车队"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char * words=" 特别车队狮哥猴警官老狒 ";
int i,j=;
j=strlen(words)-;
while(isspace(words[i]) && words[i]!='\0')
{
i++;
}
while(isspace(words[j]) && words[j]!='\0')
{
j--;
}
int n=j-i; printf("%d\n",n);
while(i<=j){
printf("%c",words[i]);
i++;
}
printf("\n%s\n",words);
return ;
}
- 一个去除首尾空格的例子
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *wants=" 石锅拌饭和辣牛肉汤想吃了斯密达 ";
int i,j=;
j=strlen(wants)-;
while(isspace(wants[i]))
{
i++;
}
while(isspace(wants[j]))
{
j--;
}
int n=j-i+;
char puluosimiga[]={};//显示分配内存空间,在栈区,如果不做这步将无法复制字符
wants+=i;
strncpy(puluosimiga,wants,n);
puluosimiga[j]='\0';
printf("%d\n",n);
printf("%s\n",puluosimiga);
return ;
}

- 字符串逆序一切看似完美无缺实则已经出错
#include<stdio.h>
#include<string.h>
int main()
{
char *words=" plz check the number or you call again ";//造成问题的原因是,这样一来就生成了一个常量,其值不可修改
int lenofwords=strlen(words)-;
printf("%p\n",words);
char *p=words+lenofwords;
printf("%p\n",lenofwords);
int i=;
while(words<p)
{
char temp=*words;
*words=*p;
*p=temp;
p--;
words++;
}
printf("%s\n",words);
return ;
}
- 解决办法,声明一个字符数组就相当于在栈上显示开辟了空间,其值即可修改
#include<stdio.h>
#include<string.h>
#include<locale.h>
#include<stdlib.h> int main()
{
char mywords[]="spending my time,my time, my time";
char *p=mywords;
char *q = p+strlen(mywords)-;
while(p<q)
{
char temp =*p;
*p=*q;
*q=temp;
q--;
p++;
}
printf("%s\n",mywords);
return ;
}
输出结果:

- 利用递归函数的压栈和出栈特性----字符数组的每一个元素随着函数的调用压栈,函数执行完毕,层层返回是出栈,出栈时打印元素
#include<stdio.h>
#include<string.h>
#include<locale.h>
#include<stdlib.h> void getnewords(char *p,char* dest)
{
if(p==NULL)
{
return;
}
if(*p=='\0')
{
return;
} getnewords(p+,dest+);
*dest=*p;
printf("%c",*p);
} int main()
{
char mywords[]="spending my time,my time, my time";
char *p=mywords;
char mydest[]={};
char *q=mydest;
//memset(q,0,sizeof(mywords));
getnewords(p,q);
printf("\n赋值后的数组%s",mydest);
return ;
}
输出结果:

压栈的效果出来了,而我们期待的字符逆序输出并未实现,你能猜到为什么吗?
逆序打印出了字符,赋值是从后往前(目标数组的从后往前),而打印数组的值的时候是从前往后。
如何返回字符数组(字符串)逆序?解决方案------通过:strncat()函数完成数组连接
#include<stdio.h>
#include<string.h>
#include<locale.h>
#include<stdlib.h> void getnewords(char *p,char* dest)
{
if(p==NULL)
{
return;
}
if(*p=='\0')
{
return;
}
getnewords(p+,dest);
strncat(dest,p,);
//printf("%c",*p);
} int main()
{
char src[]="I don't like the drugs, but the drugs like me";
char dest[]={};
printf("\nbefore:%s\n",dest);
printf("\n%s\n","==================");
getnewords(src,dest);
printf("\nfinally:%s\n",dest);
return ;
}
输出结果:

c字符数组之两头堵模型的更多相关文章
- C语言 字符串操作两头堵模型
//字符串操作两头堵模型练习 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #inc ...
- Base-64 字符数组或字符串的长度无效等问题解决方案
项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...
- 进制转换( C++字符数组 )
注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...
- 字符数组和string判断是否为空行 NULL和0 namespace变量需要自己进行初始化
string 可以这样判断空行input !="" 字符数组可以通过判断第一个元素是否为空字符'\0',是的话为空行arrar[0]=='\0':或者用长度strlen(char ...
- strlen 字符型数组和字符数组 sizeof和strlen的区别 cin.get(input,Arsize)
strlenstrlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值 ...
- [c语言]字符数组、字符串定义
C语言中字符串通常用字符指针和字符数组来定义: char* pS="abcdef"; char s[]="abcdef"; 这两种方式都会在结尾为字符串隐式补结 ...
- 计算字符数组长度,用strlen 与 sizeof 的原理与区别
遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...
- Delphi字符串与字符数组之间的转换(初始化的重要性)
紧接着上篇博客讲解的内容: 将Char型数组转换为string类型还有下面的这种方法 但是我在测试的时候遇到了一些问题,并在下面进行了解释和总结 先说出我的总结 其实我们在学习编程的时候(比如我之前学 ...
- Delphi的字符串、PChar和字符数组之间的转换
参考:http://my.oschina.net/kavensu/blog/193719 以下的各种方法都是我在Delphi 6的环境下测试成功的,可能根据你的开发环境.不同的上下文语境……有一些可能 ...
随机推荐
- word 转化为PDF 目录没了
https://jingyan.baidu.com/article/ca00d56c5d5f8de99febcf54.html 点文件-->导出-->创建PDF-->选项--> ...
- AOP+Redis锁防止表单重复提交
确保分布式锁同时满足以下四个条件 1.互斥性.在任意时刻,只有一个客户端能持有锁 2.不会发生死锁.即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户端能加锁 3.具有容错性.只要 ...
- [转帖]armel、armhf和arm64
armel.armhf和arm64 转帖 1 这些名词是什么的缩写 1.1 armel 是arm eabi little endian的缩写.eabi是软浮点二进制接口,这里的e是embeded,是对 ...
- mysql_重置密码
# 修改编码 ```pythonshow variables like '%char%'; #查看当前使用的编码 1.打开配置文件: vim /etc/mysql/my.cnf 2.在[client] ...
- uni-app项目导入第三方组件库muse-ui
你说uni-app是什么 我说,uni-app是一套基于vue.js开发跨平台应用的前端框架,可编译多个平台,比如:Android.IOS.H5.微信小程序.支付宝小程序.头条小程序.百度小程序 懂行 ...
- HTTPDNS
传统 DNS 缺点 1.域名缓存问题 它可以在本地做一个缓存,也就是说,不是每一个请求,它都会去访问权威 DNS 服务器,而是访问过一次就把结果缓存到自己本地,当其他人来问的时候,直接就返回这 ...
- ssh免秘钥
用过好几次免秘钥,但是每次都会忘了应该把copy谁的公钥到另外用户的.ssh文件夹 这里专门记录一次 注意点: A要使用ssh免密登录到B用户下(可以使远程服务器),就把A的用户下的.ssh文件的id ...
- 【洛谷 P2444】 [POI2000]病毒(AC自动机)
题目链接 这么多字符串,肯定是自动机啦. 先建出AC自动机,然后怎么表示一个安全代码没有病毒代码呢? 就是存在一条路径不经过有病毒代码段结尾的节点呗. 所以呢?有环啊!dfs一下救星了. #inclu ...
- Logback+ELK+SpringMVC搭建日志收集服务器
(转) 1.ELK是什么? ELK是由Elasticsearch.Logstash.Kibana这3个软件的缩写. Elasticsearch是一个分布式搜索分析引擎,稳定.可水平扩展.易于管理是它的 ...
- mac 上使用 idea 上传项目代码到阿里云git上
1.Idea 打开需要上传的项目 2.先在本地创建一个git仓库 VCS --> Import into Version Control --> Create Git reposito ...