i need to insert a character on a string . for example input is : cafe the output should be caxzfe . i insert xz . i just know how to concatenate two strings but when inserting a character in a string im totally lost .

Recommended Answers

All 2 Replies

Please post your code so we can see your effort on trying to solve your own problem

It's like your asking us for an immediate answer the way you say it :(

i just know how to concatenate two strings but when inserting a character in a string im totally lost .

Well, think about it as if the characters were boxes lined up. If you want to put a box somewhere in the middle, you need to make room somehow. Usually the two methods of making room are swapping an existing box with the new box, and shoving boxes out of the way.

Since order matters, shoving boxes works better:

Given a row of boxes
[c][a][f][e]

Find where you want to place new boxes
[c][a]|[f][e]

Shove boxes out of the way to make room
[c][a]      [f][e]

Set the new box(s) down
[c][a][x][z][f][e]

This process is easily translated to an algorithm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.