zoukankan      html  css  js  c++  java
  • 去除给定的字符串中左边、右边、中间的所有空格的实现

    #include<stdio.h>
    #include<string.h>
    #include<memory.h>
    #include<ctype.h>//字符类型判断函数头文件,新的空格判断函数isspace()

    char *TrimString(char *Dest)
    {
    int n,nlen;
    if(Dest!=NULL)
    {
    //以下删除字符串右边的空格
    for(n=strlen(Dest);n>0;n--)
    {
    if(!isspace(Dest[n-1])) break;
    }
    Dest[n]='\0';

    //以下删除字符串左边的空格
    nlen=strlen(Dest);
    for(n=0;n<nlen;n++)
    {
    if(!isspace(Dest[n]))
    break;
    }
    strcpy(Dest,Dest+n);
    //以下删除字符串中间的空格
    while(1)
    {
    char q[100];
    int n;
    for(n=0;n<strlen(Dest) && Dest[n]!='\0';n++)
    {

    memset(q,0,sizeof(q));
    while(isspace(Dest[n]))
    {
    strcpy(q,Dest+n+1);
    Dest[n]='\0';
    strcat(Dest,q);
    }
    }
    if(Dest[n]=='\0')
    break;
    }


    }
    return Dest;
    }

    int main()
    {
    char string[]=" ya n g b i n g ";
    TrimString(string);
    printf("%s\n",string);
    return 0;
    }

    /**
    函数名:ZipProLastSpace
    功能 :去掉前导空格和结尾空格
    参数 :
    字符串
    返回值:
    0/-1
    **/

    int ZipProLastSpace(char *str){
        int nBeginFlag=0;
        int i,j; /*两个游标*/
        i=j=0;
        while(str[i]!='\0'){
            if(str[i]==' ' || str[i]=='\t'){
                /***空格***/
                if(!nBeginFlag){
                    /***前导空格,继续****/
                    i++;
                    continue;
                }else{
                    /***非前导空格****/
                    str[j++]=str[i++];
                    continue;
                }
            }else{
                /***正常***/
                nBeginFlag=1;
                str[j++]=str[i++];
                continue;
            }
        }
        /****压缩最后的空格****/
        str[j]='\0';
        j--;
        while(j>0){
            if(str[j]==' '){
                str[j]='\0';
            }else{
                break;
            }
            j--;
        }
    
        return(0);
    }
  • 相关阅读:
    定义serialVersionUID的作用与意义整理
    HttpClient学习整理
    Eclipse+TestNG搭建接口自动化测试框架
    Jmeter之Bean shell使用(一)
    吴军博士的《数学之美》(摘录)
    SqlServer—大话函数依赖与范式
    MySQL—FOREIGN KEY
    MYSQL-用户操作
    WAMPServer 默认安装启动后,图标显示橙黄色
    Linux time命令
  • 原文地址:https://www.cnblogs.com/sherlockhomles/p/3088995.html
Copyright © 2011-2022 走看看