0

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 .

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Narue
0

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 :(

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.