zoukankan      html  css  js  c++  java
  • 字符串的反转,替换,删除

    /*
     * @Issue:  1) 字符串逆转:从键盘读入一个字符串,编程实现将字符串逆转
                    要求编制函数 void reverse(char *str);
                2)字符替换:从键盘读入一个字符串及两个字符,编程实现字符串中字符替换
                    要求编制函数 void replace(char *str, char a, char b); 将字符串str中的字符a替换为字符b,
                3)字符删除:从键盘读入一个字符串及一个字符,编程实现将给定字符串中这个字符删除,
                    要求编制函数 int delete(char *str, char a); 将字符串 str 中的 a 字符删除,
                    函数返回总共删除的字符的个数。
     * @Author: 一届书生
     * @LastEditTime: 2020-02-21 16:21:23
     */
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    // 字符翻转
    void reverse(char *str){
        char *p=str+strlen(str)-1;
        while(str<p){
            swap(*str++,*p--);
        }
    }
    
    // 字符替换
    void replace(char *str,char a,char b){
        char *p=str;
        while(p<str+strlen(str)){
            if(*p==a)*p=b;
            p++;
        }
    }
    
    // 字符删除
    int de(char *str, char a){
        int cnt=0;
        char *p=str,*q=str;
        while(*p!=''){
            if(*p!=a)
                *q++=*p++,cnt++;
            else 
                p++;
        }
        *q='';
        return cnt;
    }
    
    int main(){
        char s[20],a,b;
        cin>>s;
        reverse(s);
        cout<<"反转后字符串"<<endl<<s<<endl;
        cout<<"输入两个单词对进行完反转后的字符串进行替换:"<<endl;
        cin>>a>>b;
        replace(s,a,b);
        cout<<"替换单词后:"<<s<<endl;
        char c;
        cout<<"输入一个单词对进行完替换后的字符串进行删除:"<<endl;
        cin>>c;
        cout<<"删除后单词个数:"<<de(s,c)<<endl;
        cout<<"删除单词后字符串:"<<s<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    public static void Invoke (Action action)
    C#编写WIN32系统托盘程序
    C#的互操作性:缓冲区、结构、指针
    SQLServer异步调用,批量复制
    Python体验(10)-图形界面之计算器
    Python体验(09)-图形界面之Pannel和Sizer
    Python体验(08)-图形界面之工具栏和状态栏
    Python体验(07)-图形界面之菜单
    C#利用WIN32实现按键注册
    Javascript猜数字游戏
  • 原文地址:https://www.cnblogs.com/52dxer/p/12341845.html
Copyright © 2011-2022 走看看