zoukankan      html  css  js  c++  java
  • 对于特殊字符串的处理方法

    对于特殊的字符串,我们对字符串进行特殊与非特殊两种,第一种字符串开头特殊字符提到末尾处理方法或者末尾字符串提到开头,总之先处理特殊的就OK了

    开头提到末尾处理:

    #include "stdio.h"
    #include"stdlib.h"
    #include "conio.h"
    void fun(char*a)
    {
    int i=0,j=0;
    char *p=a;
    while(*p&&*p=='*') //单独处理字符串前的*字符,并保存到a指针里去
    {    a[i++]=*p;    
    p++;
    }
    while(*p)
    {
    a[j++]=*p;
    p++;
    a[i++]=a[j]; //开始衔接 
    }
    
    a[i]=''; // 加上结束标志
    }
    main()
    {
    char s[80];
    printf("printf the string :
    ");
    gets(s);
    fun(s);
    printf("The string after deleted %c :
    ");
    puts(s); 
    }

    结果截图:

      末尾提到开头处理:
     通过一个for循环直接指向字符串的最后一个字符,再减1指向最后一个了非空字符

    /* Note:Your choice is C IDE */
    #include "stdio.h"
    #include "conio.h"
    void fun(char *a)
    
    char *p;
    
    *p=a;
    
    int i=0,j=0;
    { while(*a!='')
    a++;
    a--;
    //直接指向字符串末尾
    while(*a=='*')
    {
    
    a[i++];  //把末尾的*保存在a[i],a[j]保存删除后的字符串,最后衔接起来
    
    a[j--];
    
    }
    
     
    
    while(a[i]!='')
    
    a[j++]=a[j]
    
    // 长度加1加结束符
    a[j]='';
    }
    
    void main()
    {char s[100];
    printf("Enter the string :
    ");
    gets(s);
    fun(s);
    printf("The string after deleted :
    ");
    puts(s);
    
    
    }
    
    处理开头字符与结尾字符串:方法是在主函数中找出特殊字符结束的位置,通过形式参数返回自定义函数从不是要删除的字符串开始保存在新的指针里,返回值
    
    /* Note:Your choice is C IDE */
    #include "stdio.h"
    #include "conio.h"
    void fun (char *a,int n,int h,int e)
    {
    int i,j=0;
    for(i=h;i<n-e;i++)
    a[j++]=a[i];
    a[j]='';
    }
    void main()
    { char s[81],*t,*f;
    int m=0,tn=0,fn=0;
    printf("Enter a string:
     ");
    gets(s);
    t=f=s;
    while(*t)
    {
    t++;
    m++;
    }
    t--; //指向最后一个字符
    while(*t=='*')
    {
    t--;
    tn++; //统计末尾字符*的个数
    }
    while(*f=='*')
    {
    f++;
    fn++;
    }  //统计开头字符*的个数
    fun(s,m,fn,tn);
    printf("The string after deleted :
    ");
    puts(s); 
    }
  • 相关阅读:
    监听器
    过滤器
    连接池与分页
    jdbc优化
    jdbc入门
    web开发mysql基础
    自定义标签
    jsp基础
    会话管理入门
    19. Remove Nth Node From End of List C++删除链表的倒数第N个节点
  • 原文地址:https://www.cnblogs.com/Mr210843013/p/4783161.html
Copyright © 2011-2022 走看看