• C语言小程序:除去字符串中间不需要的字符(从小引发大思考)


    #include <stdio.h>
    #include <conio.h>

    void fun(char *a, char *h, char *p)
    {
     char b[81];
     char *c = a;
     int i = 0;
     while (*c){
      if (c <= h){
       b[i] = *c;
       i++;
       }
      else if (h < c && c <= p){
          if (*c != '*'){
          b[i] = *c;
          i++;
          }
          }
       else{
         b[i] = *c;
         i++;
         }                                      
       c++;
      }
     for (int j = 0; j < i; j++){
      *a = b[j];
      a++;
     }
     *a = '';   //puts()遇到空字符停止,空字符不是空白字符,空白字符是指:空格、制表符 等不显示的字符,而空字符‘’往往作为字符串的停止字符
    }

    /* fun()函数如果不加最后的 : *a = ''; 将会输出“*****FGHhhhHHW***HHW***",总长度不变;加上这句之后,输出”*****FGHhhhHHW***",相当于把字母中间的'*'删除了,达到目的;

    int main()

    {

    char s[81], *t, *f;
     printf("Enter a string: ");

    gets(s);    //输入一个字符串,形如"*****FG*H*hhh****HHW***"
     t = f = s;
     while (*t) t++;
     t--;
     while (*t == '*') t--;        //使 t 指向最后输入字符串的最后一个字母‘W'
     while (*f == '*') f++;     //使 f 指向输入字符串的第一个字母’F'
     fun(s, f, t);                    //调用函数除去字母间的‘*’,使字符串变为”*****FGHhhhHHW***"
     printf("The string after deleted: ");

    puts(s);                         //输出字符串,puts()遇到空字符‘'停止输出;
     return 0;
    }

  • 相关阅读:
    算法-回溯法
    算法-动态规划=背包问题
    算法-贪心算法
    算法-KMP模式匹配算法
    算法-两点之间最短路径
    Hibernate学习笔记
    MyBatis一级缓存和二级缓存
    使用MyBatis-Gererator自动生成Dao.Model.Mapping相关文件
    MyBatis中一对多和多对一的学习详解
    MyBatis中使用添加判断进行查询
  • 原文地址:https://www.cnblogs.com/warmbeast/p/6607513.html
Copyright © 2020-2023  润新知