2、删除字符(函数重载)

问题描述 :

内容:对于字符串,实现以下删除字符的函数,并编写main函数测试这些函数:
实验目的:学习函数重载的使用。

1. remove函数:
从形参传入一个字符,将该字符从字符串中删除。

2.remove函数:
从形参传入一个下标index,将index处的字符从字符串中删除。

3.remove函数:
从形参传入两个下标startIndex和endIndex,将从startIndex到endIndex范围内的字符从字符串中删除。

 

main函数可参考以下代码编写:

int main()
{
 int num, index, startIndex, endIndex;
 char delChar;
 string src, dest;

 while(cin >> num)
 {
  switch(num)
  {
   case 1:
    cin >> src >> delChar;
    cout << remove(src, delChar) << endl;
    break;
   case 2:
    cin >> src >> index;
    cout << remove(src, index) << endl;
    break;
   case 3:
    cin >> src >> startIndex >> endIndex;
    cout << remove(src, startIndex, endIndex) << endl;
    break;
  }
 }
 return 0;
}

 

输入说明 :

输入时,每组测试数据包含两行,第一行输入一个整数,指定需要完成的操作,第二行为该操作需要的数据。
对于每个整数对应的操作及其相应数据的输入方式如下(输入的字符串中不包含空格):
1:对应remove,第二行输入字符串和一个字符(以空格分隔),将字符从字符串中删除。

2:对应remove,第二行输入字符串和index(以空格分隔),将index处的字符从字符串中删除。
异常处理:如果index不合法,则不删除字符。

3:对应remove,第二行输入字符串和startIndex、endIndex(以空格分隔),将从startIndex到endIndex范围内的字符从字符串中删除。
异常处理:如果startIndex、endIndex不合法,则不删除字符。

输出说明 :

对于每组测试数据,输出对应的结果。如果结果为空字符串,则输出一个空行。

每行行首与行尾无多余空格,第一行之前与最后一行之后无多余空行。

 

输入范例 :

1
acdef b
2
abcdef 3
2
abcdef 6
3
abcd 0 3
3
abcdef 3 5
3
abcdef 3 7

输出范例 :

acdef
abcef
abcdef

abc
abcdef
 

题解代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
string remove(string s, char c)
{
    //delet the char 
    int i;
    string ans = "";
    for (i = 0; i < s.length(); i++)
        if (s[i] != c)
            ans += s[i];
    return ans;
}
string remove(string s, int n)
{
    //delet the char 
    int i;
    int l = s.length();
    l--;
    if (n > l || n < 0)
        return s;
    string ans = "";
    for (i = 0; i < n; i++)
        ans += s[i];
    for (i = n + 1; i <= l; i++)
        ans += s[i];
    return ans;
}
string remove(string s, int x, int y)
{
    //delet the char 
    int i;
    int l = s.length();
    l--;
    if (x > l || x < 0 || y>l || y<0 || x>y)
        return s;
    string ans = "";
    for (i = 0; i < x; i++)
        ans += s[i];
    for (i = y + 1; i <= l; i++)
        ans += s[i];
    return ans;
}
int main()
{
    int num, index, startIndex, endIndex;
    char delChar;
    string src, dest;

    while (cin >> num)
    {
        switch (num)
        {
        case 1:
            cin >> src >> delChar;
            cout << remove(src, delChar) << endl;
            break;
        case 2:
            cin >> src >> index;
            cout << remove(src, index) << endl;
            break;
        case 3:
            cin >> src >> startIndex >> endIndex;
            cout << remove(src, startIndex, endIndex) << endl;
            break;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值