zoukankan      html  css  js  c++  java
  • 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=0;
        j=strlen(words)-1;
        while(isspace(words[i]) && words[i]!='')
        {
            i++;
        }
        while(isspace(words[j]) && words[j]!='')
        {
            j--;
        }
        int n=j-i;
    
        printf("%d
    ",n);
        while(i<=j){
            printf("%c",words[i]);
            i++;
        }
        printf("
    %s
    ",words);
        return 0;
    }
    • 一个去除首尾空格的例子
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
        char *wants="    石锅拌饭和辣牛肉汤想吃了斯密达    ";
        int i,j=0;
        j=strlen(wants)-1;
        while(isspace(wants[i]))
        {
            i++;
        }
        while(isspace(wants[j]))
        {
            j--;
        }
        int n=j-i+1;
        char puluosimiga[1024]={0};//显示分配内存空间,在栈区,如果不做这步将无法复制字符
        wants+=i;
        strncpy(puluosimiga,wants,n);
        puluosimiga[j]='';
        printf("%d
    ",n);
        printf("%s
    ",puluosimiga);
        return 0;
    }

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

    输出结果:

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

    输出结果:

     压栈的效果出来了,而我们期待的字符逆序输出并未实现,你能猜到为什么吗?

    逆序打印出了字符,赋值是从后往前(目标数组的从后往前),而打印数组的值的时候是从前往后。

    如何返回字符数组(字符串)逆序?解决方案------通过: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=='')
        {
            return;
        }
        getnewords(p+1,dest);
        strncat(dest,p,1);
        //printf("%c",*p);
    }
    
    int main()
    {
        char src[1024]="I don't like the drugs, but the drugs like me";
        char dest[1024]={0};
        printf("
    before:%s
    ",dest);
        printf("
    %s
    ","==================");
        getnewords(src,dest);
        printf("
    finally:%s
    ",dest);
        return 0;
    }

    输出结果:

  • 相关阅读:
    翻转单词顺序列
    和为S的两个数字
    单例模式
    python利用pyinstaller打包常用打包命令
    python 3.8 使用pymssql 向SQL Server插入数据不成功原因
    PyQt5(designer)入门教程
    PyQt5中文教程
    scrapy 图片爬取 多层多页 保存不同的文件夹 重命名full文件夹
    安装Python + PyCharm + PyQt5配套设置
    python用pymysql模块操作数据库MySQL,实现查增删改
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/12048563.html
Copyright © 2011-2022 走看看