How to remove N characters from the middle of a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>

int main() {
    std::string str = "abc123def";
    
    int mid = str.length() / 2;
    int N = 3;
    
    str = str.substr(0, mid - N / 2) + str.substr(mid + 1 + N / 2);
    
    std::cout << str << std::endl;
}

  
  
/*
run:
  
abcdef
 
*/

 



answered Sep 11, 2024 by avibootz

Related questions

2 answers 114 views
1 answer 108 views
1 answer 119 views
1 answer 133 views
...