none
How to add a certain charachter to each letter of a string RRS feed

  • Question

  • I have an string, (for eg input1) which contains some text, I would like to add a character (eg ".") to every letter of the string for a certain number.

    // user insetrs text string usertext = textbox1example.text; //user specifies count of combination string usercount = textbox2example.text; //the problem I have, if usertext contains "hello" and

    usercount contains 7 the expected result will be h.ello , he.llo , hel.lo, hell.o

    h..ello, he..llo, hel..lo.


    Friday, January 10, 2020 6:33 PM

Answers

  •  app that someone asked me to create.

    I note that you didn't explicitly deny that this is a homework assignment.
    The "someone" who asked you to write the code could well be an instructor
    or teacher for all we know.

    I'll give you the benefit of any doubt and offer a very quick and probably
    sub-optimal example of one way to get the results you showed in your sample.
    It may well need refinement, debugging, optimizing, etc. As usual in such
    cases, be prepared to explain how it works if submitting it in a class, as
    well as acknowledging the source.

    static void Main(string[] args)
    {
        int count = 7, pos = 1;
        string str = "hello", insertChar = ".", insert = insertChar, s;    
        for(int n = 0; n < count; ++n)
        {
            s = str.Insert(pos++, insert);
            Console.WriteLine(s);
            if(pos == str.Length)
            {
                pos = 1;
                insert += insertChar;
            }
        }
    }
    

    Output:

    h.ello
    he.llo
    hel.lo
    hell.o
    h..ello
    he..llo
    hel..lo

    - Wayne

    • Marked as answer by Helios Lucifer Saturday, January 11, 2020 7:55 AM
    Friday, January 10, 2020 11:32 PM

All replies

  • Hello,

    I'm curious, what is the actual/real purpose that this can be used for in real life? Almost seems like school homework.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 10, 2020 7:32 PM
    Moderator
  • Thanks for your reply, this is an email multiplier app that someone asked me to create and yet I don't have an Idea on how to create it.
    Friday, January 10, 2020 9:43 PM
  •  this is an email multiplier app that someone asked me to create and yet I don't have an Idea on how to create it.

    What is "an email multiplier app"? It sounds like something a spammer or a 
    hacker might use.

    How does creating variations of the string with shifting periods aid the
    intended functionality of the app?

    >I don't have an Idea on how to create it.

    Then why did you accept the task? Are you now going to try and "outsource" it
    by getting forum participants to write all of the code for you? As a general
    practice, we don't usually do custom programming in these forums. That's
    how many participants earn a living.

    As a pedantic aside: it appears that you may have missed a pattern in your
    example:

    "usercount contains 7 the expected result will be h.ello , he.llo , hel.lo, hell.o
    h..ello, he..llo, hel..lo.:"

    If the task requires "add a character (eg ".") to every letter of the string"
    then shouldn't there be a pattern with a period after the 'o'? e.g. -

    h.ello , he.llo , hel.lo, hell.o, hello., ...

    - Wayne

    Friday, January 10, 2020 10:26 PM
  •  app that someone asked me to create.

    I note that you didn't explicitly deny that this is a homework assignment.
    The "someone" who asked you to write the code could well be an instructor
    or teacher for all we know.

    I'll give you the benefit of any doubt and offer a very quick and probably
    sub-optimal example of one way to get the results you showed in your sample.
    It may well need refinement, debugging, optimizing, etc. As usual in such
    cases, be prepared to explain how it works if submitting it in a class, as
    well as acknowledging the source.

    static void Main(string[] args)
    {
        int count = 7, pos = 1;
        string str = "hello", insertChar = ".", insert = insertChar, s;    
        for(int n = 0; n < count; ++n)
        {
            s = str.Insert(pos++, insert);
            Console.WriteLine(s);
            if(pos == str.Length)
            {
                pos = 1;
                insert += insertChar;
            }
        }
    }
    

    Output:

    h.ello
    he.llo
    hel.lo
    hell.o
    h..ello
    he..llo
    hel..lo

    - Wayne

    • Marked as answer by Helios Lucifer Saturday, January 11, 2020 7:55 AM
    Friday, January 10, 2020 11:32 PM
  • > Thanks a million times, this was a bonus assignment for my C# class but I didn't want it to seem like homework, Yet you helped me a lot and I am gratefull.
    Saturday, January 11, 2020 7:56 AM