• 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字符数组之两头堵模型的更多相关文章

  1. C语言 字符串操作两头堵模型

    //字符串操作两头堵模型练习 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #inc ...

  2. Base-64 字符数组或字符串的长度无效等问题解决方案

    项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...

  3. 进制转换( C++字符数组 )

    注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...

  4. 字符数组和string判断是否为空行 NULL和0 namespace变量需要自己进行初始化

    string 可以这样判断空行input !="" 字符数组可以通过判断第一个元素是否为空字符'\0',是的话为空行arrar[0]=='\0':或者用长度strlen(char ...

  5. strlen 字符型数组和字符数组 sizeof和strlen的区别 cin.get(input,Arsize)

    strlenstrlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值 ...

  6. [c语言]字符数组、字符串定义

    C语言中字符串通常用字符指针和字符数组来定义: char* pS="abcdef"; char s[]="abcdef"; 这两种方式都会在结尾为字符串隐式补结 ...

  7. 计算字符数组长度,用strlen 与 sizeof 的原理与区别

    遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...

  8. Delphi字符串与字符数组之间的转换(初始化的重要性)

    紧接着上篇博客讲解的内容: 将Char型数组转换为string类型还有下面的这种方法 但是我在测试的时候遇到了一些问题,并在下面进行了解释和总结 先说出我的总结 其实我们在学习编程的时候(比如我之前学 ...

  9. Delphi的字符串、PChar和字符数组之间的转换

    参考:http://my.oschina.net/kavensu/blog/193719 以下的各种方法都是我在Delphi 6的环境下测试成功的,可能根据你的开发环境.不同的上下文语境……有一些可能 ...

随机推荐

  1. Prometheus入门到放弃(5)之AlertManager部署

    alertmanager与exporters.cadvisor一样,都是独立于prometheus项目,这里我们也使用docker方式部署alertmanager. 1.下载镜像 镜像地址:https ...

  2. 常用Tables控件介绍(二)

    初始化:1.使用现有表单创建数据表格,定义在HTML中的字段和数据 2.使用现有的table创建数据表格,定义在HTML中的字段 3.使用JS创建数据库表格 一.初始化后,根据单元格内的值,修改显示内 ...

  3. python学习-69 包装和授权

    包装 1.二次加工标准类型(包装) class List(list): def append(self, a_objcet): if type(a_objcet) is str: super().ap ...

  4. [LOJ2541] [PKUWC2018] 猎人杀

    题目链接 LOJ:https://loj.ac/problem/2541 Solution 很巧妙的思路. 注意到运行的过程中概率的分母在不停的变化,这样会让我们很不好算,我们考虑这样转化:假设所有人 ...

  5. 文件属性的生成操作build action

    文件属性的生成操作build action Action 说明 None 资源既不会被集成到程序集内,也不会打包到xap包中.不过我们可以通过设置CopyToOutputDirectory选项让其自动 ...

  6. ADO.NET 三(Command)

    操作数据库需则要用到 Command 类中提供的属性和方法.下面来介绍一下如何使用 Command 类来操作数据表中的数据. Command 类概述 在 System.Data.SqlClient 命 ...

  7. MonoSymbolFileException in CheckLineNumberTable

    Mono.CompilerServices.SymbolWriter.MonoSymbolFileException: Exception of type 'Mono.CompilerServices ...

  8. vue 自定义image组件

    介绍 1:当图片加载失败时,给出错误提示. 2:当图片加载中时,给出加载提示. 3:图片处理模式:等比缩放/裁剪/填充/... 1.图片加载状态处理 通过给图片绑定load事件与error事件处理函数 ...

  9. Switch开关在element-ui表格中点击没有效果解决方法

    <el-table-column label="三审" align="center"> <template slot-scope=" ...

  10. Python2 和 pip2 存在, Python3 也存在,但是 pip3 不存在的解决办法

    sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3 get-pip.py 输入两行命令即可