• 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. PHP 获取星期

    <?php function getWeek($time = 0) { $week_array=array('日', '一', '二', '三', '四', '五', '六'); //先定义一个 ...

  2. Go语言( 函数)

    函数是组织好的.可重复使用的.用于执行指定任务的代码块.本文介绍了Go语言中函数的相关内容. 函数 Go语言中支持函数.匿名函数和闭包,并且函数在Go语言中属于“一等公民”. 函数定义 Go语言中定义 ...

  3. 『Go基础』第5节 第一个Go程序

    本节我们来学习写一个最简单的Go程序: 打印 Hello Go. 第一个Go程序, 只要跟着做, 留下个印象就可以. 用Goland创建一个 hello_go.go 文件(后缀为 .go ). 文件内 ...

  4. lmir 随笔

    近期需要研究一些特征工程的工作,就打算把微软之前公布出来的特征都复现一遍,今天遇到的特征是 LMIR, 其实也就是language model for information retrieval的简写 ...

  5. [洛谷P4052][JSOI2007]文本生成器

    题目大意:有$n$个字符串$s_i$,问有多少个长度为$m$的字符串至少包含$n$个字符串中的一个,字符集 A-Z .$s_i,m\leqslant100,n\leqslant60$ 题解:$AC$自 ...

  6. go语言学习---使用os.Args获取简单参数(命令行解析)

    实例1: //main package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args ...

  7. pytorch learning rate decay

    关于learning rate decay的问题,pytorch 0.2以上的版本已经提供了torch.optim.lr_scheduler的一些函数来解决这个问题. 我在迭代的时候使用的是下面的方法 ...

  8. 7 批量查询mget、批量修改bulk

    注意:当执行多条数据查询.增删改时,一定要用mget.bulk,提升性能,减少网络传输   mget   回顾:查询单个文档 GET /beauties/my/2   mget 查询多个文档: 不同 ...

  9. python-socket并发-解决tcp粘包问题

    粘包问题 tcp协议才会有粘包问题,udp协议没有粘包问题. 因为tcp协议是将需要传输的内容先读入缓存里,然后在一点点传,受接收方字符限制,并不能一次传输完成,第二次就会将第一次剩下的部分+第二次的 ...

  10. Layui学习笔记(一)—— 关于模块的扩展

    在使用layui的时候,总有官方自带模块不够用想自己扩展的时候,这时候我们就需要扩展模块了. 模块扩展有两种: (一)普通地扩展 layui.define( function (exports) { ...