Home > OS >  How can we insert a character to a string in c ?
How can we insert a character to a string in c ?

Time:03-07

INPUT STRING

ABCE

INPUT CHAR

D

OUTPUT STRING

ABCDE

It should run for all sizes , and it should be a standard procedure ie run for all the cases. Can anyone Help me with this? Any help would be appreciated.

CodePudding user response:

You should try the std::string::insert(..). Check out the documentation

#include <iostream>
#include <string>

int main() {
    std::string str("ABCE");
    std::cout << str << std::endl; // ABCE

    str.insert(3, 1, 'D');
    std::cout << str << std::endl; // ABCDE

    return -1;
}
  • Related