a program that deletes specific characters in a string

Asked 1 years ago, Updated 1 years ago, 31 views

I'm sorry.No matter how you think about it, I can't get an answer, so I'll use everyone's help here.

Describe a program that removes certain characters from the string.
// Delete the character that has been substituted for the variable code from the variable str.

#include<iostream>
# include <iomanip>
# include <conio.h>
using namespace std;

int main()
{
    // variable declaration
    char str[10]="";
    char code='\0';
    

    // Enter the string and characters to delete
    cout<<"To delete:";
    cin>>str;
    cout<<"Characters to delete:";
    cin>>code;

    // Delete characters from the entered string
    
    // Output results
    cout<<"Deleted string:"<<str;

    _getch();
    return 0;
}

c++

2022-09-30 19:45

2 Answers

How about using std::remove


# include <algorithm>
# include <cstring>
                :

  // Delete characters from the entered string
  *remove(str, str+strlen(str), code) = 0;

  // Output results
  cout<<"Deleted string:"<<str<<endl;//endl


2022-09-30 19:45

#include<iostream>
# include <iomanip>
using namespace std;

int main() {
    char str[10] = "teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
    char code = 'e';

    for(inti=0;i<sizeof(str)/sizeof(*str);++i){
        if(str[i]==code){
            for(int j=i;j<sizeof(str)/sizeof(*str)-1;++j){
                str[j] = str[j+1];
            }
            --i;
        }
    }
    cout<<str<<endl;
    return 0;
}

It was pointed out, so I corrected it


2022-09-30 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.