how to insert a single character using insert() function

 converting a character into string type :

we will use string() function to convert  a character into string in c++.

suppose char c ='a' ;

  now ,  to convert it to string , we use : 

            string s = string(1 , c );            .............................................(1)

   now , we have a new string s .



cout << s;

output : a [of type string].


Inserting charater using insert() after converting into string type :

now , insert(some_index , string_to_be_inserted);

in insert() . second parameter  must be  a string , so if we  want to insert a single character using insert() function , we will use 

insert(some_index , string(1,char_variable));

for example :

 int main()

{

 string s = "63753";

 char b = '8';

s.insert(3,string(1,b));

cout << s ;

}


output :  637853


if you have any doubt , then ask in comment section.




Comments