Remove and Add letters in Strings











up vote
2
down vote

favorite












I am trying to solve a problem in C#.



Here is the task:





  • If a word begins with a vowel (a, e, i, o, u or A, E, I, O, U), remove
    the first letter and append it to the end, then add "che". If you have
    the word “orange” It translates to “rangeoche”

  • If a word begins with a consonant (i.e. not a vowel), append "che" to the end of the word. For example, the word "chicken" becomes
    "chickenche".

  • If the word has even number of letters append one more "e" to the end of it.


Print the translated sentence.




Example:



Hello there Amy


Output:



Helloche thereche myAche


Here is what I have done so far :



        string Sentence = Console.ReadLine();
string output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = new StringBuilder(Sentence);
foreach (string s in output)
{
letter = s[0];

if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
}
else
{
sb.Insert(s.Length,che);
// Console.WriteLine("first char of a word is a consonant");
}

if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;

}
Console.WriteLine(sb);


The problem is I cannot add "che" or remove vowels of words because the index is moving due to those changes. I can only change the first word. My ifs are correct because if I uncomment the Console.Writelines they scan through each word.
I am just struggling with the adding/removing of each word. Can you please point me to the right direction?










share|improve this question




















  • 3




    No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
    – elgonzo
    Nov 21 at 19:37












  • Note that you add "che" in all cases - so make that a common case. How do you handle y?
    – NetMage
    Nov 21 at 20:26










  • @elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
    – grozdeto
    Nov 22 at 12:02

















up vote
2
down vote

favorite












I am trying to solve a problem in C#.



Here is the task:





  • If a word begins with a vowel (a, e, i, o, u or A, E, I, O, U), remove
    the first letter and append it to the end, then add "che". If you have
    the word “orange” It translates to “rangeoche”

  • If a word begins with a consonant (i.e. not a vowel), append "che" to the end of the word. For example, the word "chicken" becomes
    "chickenche".

  • If the word has even number of letters append one more "e" to the end of it.


Print the translated sentence.




Example:



Hello there Amy


Output:



Helloche thereche myAche


Here is what I have done so far :



        string Sentence = Console.ReadLine();
string output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = new StringBuilder(Sentence);
foreach (string s in output)
{
letter = s[0];

if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
}
else
{
sb.Insert(s.Length,che);
// Console.WriteLine("first char of a word is a consonant");
}

if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;

}
Console.WriteLine(sb);


The problem is I cannot add "che" or remove vowels of words because the index is moving due to those changes. I can only change the first word. My ifs are correct because if I uncomment the Console.Writelines they scan through each word.
I am just struggling with the adding/removing of each word. Can you please point me to the right direction?










share|improve this question




















  • 3




    No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
    – elgonzo
    Nov 21 at 19:37












  • Note that you add "che" in all cases - so make that a common case. How do you handle y?
    – NetMage
    Nov 21 at 20:26










  • @elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
    – grozdeto
    Nov 22 at 12:02















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to solve a problem in C#.



Here is the task:





  • If a word begins with a vowel (a, e, i, o, u or A, E, I, O, U), remove
    the first letter and append it to the end, then add "che". If you have
    the word “orange” It translates to “rangeoche”

  • If a word begins with a consonant (i.e. not a vowel), append "che" to the end of the word. For example, the word "chicken" becomes
    "chickenche".

  • If the word has even number of letters append one more "e" to the end of it.


Print the translated sentence.




Example:



Hello there Amy


Output:



Helloche thereche myAche


Here is what I have done so far :



        string Sentence = Console.ReadLine();
string output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = new StringBuilder(Sentence);
foreach (string s in output)
{
letter = s[0];

if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
}
else
{
sb.Insert(s.Length,che);
// Console.WriteLine("first char of a word is a consonant");
}

if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;

}
Console.WriteLine(sb);


The problem is I cannot add "che" or remove vowels of words because the index is moving due to those changes. I can only change the first word. My ifs are correct because if I uncomment the Console.Writelines they scan through each word.
I am just struggling with the adding/removing of each word. Can you please point me to the right direction?










share|improve this question















I am trying to solve a problem in C#.



Here is the task:





  • If a word begins with a vowel (a, e, i, o, u or A, E, I, O, U), remove
    the first letter and append it to the end, then add "che". If you have
    the word “orange” It translates to “rangeoche”

  • If a word begins with a consonant (i.e. not a vowel), append "che" to the end of the word. For example, the word "chicken" becomes
    "chickenche".

  • If the word has even number of letters append one more "e" to the end of it.


Print the translated sentence.




Example:



Hello there Amy


Output:



Helloche thereche myAche


Here is what I have done so far :



        string Sentence = Console.ReadLine();
string output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = new StringBuilder(Sentence);
foreach (string s in output)
{
letter = s[0];

if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
}
else
{
sb.Insert(s.Length,che);
// Console.WriteLine("first char of a word is a consonant");
}

if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;

}
Console.WriteLine(sb);


The problem is I cannot add "che" or remove vowels of words because the index is moving due to those changes. I can only change the first word. My ifs are correct because if I uncomment the Console.Writelines they scan through each word.
I am just struggling with the adding/removing of each word. Can you please point me to the right direction?







c# string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 20:06









Dmitry Bychenko

104k992131




104k992131










asked Nov 21 at 19:32









grozdeto

406




406








  • 3




    No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
    – elgonzo
    Nov 21 at 19:37












  • Note that you add "che" in all cases - so make that a common case. How do you handle y?
    – NetMage
    Nov 21 at 20:26










  • @elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
    – grozdeto
    Nov 22 at 12:02
















  • 3




    No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
    – elgonzo
    Nov 21 at 19:37












  • Note that you add "che" in all cases - so make that a common case. How do you handle y?
    – NetMage
    Nov 21 at 20:26










  • @elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
    – grozdeto
    Nov 22 at 12:02










3




3




No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
– elgonzo
Nov 21 at 19:37






No need to mess around with StringBuilder, it is just a distraction. (unless you specifically are instructed to use it). Think of it as follows: If a string starts with a vowel, take the remainder of the string (behind the vowel) and add the vowel to it. Adding strings together to form a new string is simple, just use +. :-) Note that this does not require moving the vowel around in the original source string, just make a new string with the parts i mentioned...
– elgonzo
Nov 21 at 19:37














Note that you add "che" in all cases - so make that a common case. How do you handle y?
– NetMage
Nov 21 at 20:26




Note that you add "che" in all cases - so make that a common case. How do you handle y?
– NetMage
Nov 21 at 20:26












@elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
– grozdeto
Nov 22 at 12:02






@elgonzo . Thank you very much for it worked!!. I have followed your advice. Under vowels I have created string newword = s.Substring(1) + s[0] + "che"; Under consonants : string newword = s + "che"; In each case I have checked if the length of the word is even or odd and I have added if case to each condition. For example in the vowels case if the length % 2 = 0 I used Console.Write(newword + "e" + " "); else Console.Write(newword + " "); In the consosant case I followed the same logic. I had no understanding of Regex and methods yet and I was just starting with StringBuilder.
– grozdeto
Nov 22 at 12:02














7 Answers
7






active

oldest

votes

















up vote
1
down vote



accepted










I would suggest you to create StringBuilder object and append appropriate string into the IF condition. Try with the below code:



    string Sentence = Console.ReadLine();
string output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = null;
Console.WriteLine("n");
string strFinal = "";
foreach (string s in output)
{
letter = s[0];
sb = new StringBuilder(s);

if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
string s1 = sb.Remove(0, 1).ToString();
sb.Insert(s1.Length, letter);
sb.Insert(sb.Length, che);
}
else
{
// Console.WriteLine("first char of a word is a consonant");
sb.Insert(s.Length, che);
}

if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
// sb.Insert(s.Length, "e");
sb.Insert(sb.Length, "e");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;

strFinal += sb + " ";
}

Console.WriteLine(strFinal);
Console.ReadKey();





share|improve this answer























  • I like that. I will try it, as I was just learning StringBuilder.
    – grozdeto
    Nov 22 at 12:04










  • @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
    – Ali Azam
    Nov 22 at 12:15










  • thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
    – grozdeto
    Nov 22 at 12:31












  • @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
    – Ali Azam
    Nov 22 at 13:10










  • @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
    – Ali Azam
    Nov 22 at 13:12


















up vote
1
down vote













First, I'd recommend changing your translation code to have a function that acts on words, instead of the whole sentence. So the code in foreach (string s in output) should be moved to another function that just acts on that string. And don't try to manipulate the string passed it, create a new one based on the logic you've listed. Once you've created the translated string, return it to the caller. The caller would then reconstruct the sentence from each returned translation.






share|improve this answer




























    up vote
    0
    down vote













    You mustn't change your word before the end of ifs. you need to test all of the conditions and save the result in another values, finally do changes on the word:



    foreach (string s in output)
    {
    letter = char.ToLower(s[0]);
    bool isVowel = false;
    bool isEven = false;

    if (letter == 'a' || letter == 'e' || letter == 'i'
    || letter == 'o' || letter == 'u')
    {
    isVowel = true;
    }

    if (s.Length % 2 == 0)
    {
    isEven = true;
    }

    //Now you can change the word
    if (isVowel)
    {
    //Do What you want
    }

    if (isEven)
    {
    //Do What you want
    }

    //Console.WriteLine(firstchar);
    int currentWordLength = s.Length;

    }





    share|improve this answer






























      up vote
      0
      down vote













      Using the obvious extension methods:



      public static class ExtensionMethods {
      // ***
      // *** int Extensions
      // ***
      public static bool IsEven(this int n) => n % 2 == 0;

      // ***
      // *** String Extensions
      // ***
      public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
      public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
      }


      You can use LINQ to process the rules:



      var vowels = "aeiouAEIOU".ToHashSet();

      var ans = src.Split(' ')
      .Select(w => (w.StartsWithOneOf(vowels) ? w.Substring(1)+w[0] : w)+"che"+(w.Length.IsEven() ? "e" : ""))
      .Join(" ");





      share|improve this answer




























        up vote
        0
        down vote













        Let's start from splitting the initial problem into smaller ones, with a help of extract methods:



          using using System.Text.RegularExpressions;

        ...

        private static String ConvertWord(string word) {
        //TODO: Solution here
        return word; // <- Stub
        }

        private static String ConvertPhrase(string phrase) {
        // Regex (not Split) to preserve punctuation:
        // we convert each word (continued sequence of letter A..Z a..z or ')
        // within the original phrase
        return Regex.Replace(phrase, @"[A-Za-z']+", match => ConvertWord(match.Value));

        // Or if there's guarantee, that space is the only separator:
        // return string.Join(" ", phrase
        // .Split(new char { ' ' }, StringSplitOptions.RemoveEmptyEntries)
        // .Select(item => ConvertWord(item)));
        }


        Now it's time to implement ConvertWord:



          private static String ConvertWord(string word) {
        // Do not forget of special cases - e.g. empty string
        if (string.IsNullOrEmpty(word))
        return "chee";

        // If the word has even number of letters append one more "e" to the end of it.
        string suffix = word.Length % 2 == 0 ? "chee" : "che";

        // To cases: starting from vowel / consonant
        char letter = char.ToUpper(word[0]);

        if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U')
        return word.Substring(1) + word.Substring(0, 1) + suffix;
        else
        return word + suffix;
        }


        Finally



          string Sentence = Console.ReadLine();

        Console.Write(ConvertPhrase(Sentence));


        For test input



        "It's just a simple test (demo only): nothing more!"


        Will get



        t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!





        share|improve this answer



















        • 2




          Regex when it’s obviously a beginner’s homework or learning excercise?
          – InBetween
          Nov 21 at 20:21










        • @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
          – Dmitry Bychenko
          Nov 21 at 20:33






        • 1




          I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
          – InBetween
          Nov 21 at 20:57


















        up vote
        -1
        down vote













        I would say break the logic into specific units so you can write tests,



            static void Main(string args)
        {
        var input = Console.ReadLine();
        var inputs = input.Split(' ');
        var sentence = string.Join(" ", inputs.Select(ConvertWord));
        Console.Write(sentence);
        Console.Read();
        }

        internal static string ConvertWord(string input)
        {
        const string che = "che";

        var vowels = new List<string>
        {
        "a","A", "e", "E", "i", "I", "o", "O", "u", "U"
        };

        var firstChar = input.First();

        var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));

        string rule2String, output;

        if (string.IsNullOrEmpty(startsWithVowel))
        {
        output = input + che;
        }

        else
        {
        output = input.Substring(1) + startsWithVowel + che;
        }

        rule2String = IsLengthEven(input)
        ? output + "e"
        : output
        ;

        return rule2String;
        }

        internal static bool IsLengthEven(string input)
        {
        return input.Length % 2 == 0;
        }


        Hope this Helps!
        PS: I have not covered the edge cases






        share|improve this answer





















        • In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
          – elgonzo
          Nov 22 at 10:06




















        up vote
        -1
        down vote













        I would recommend to use String.Join using Linq with String.Format. Then the solution is quite easy:



            private String convertSentence() {
        var sentence = "Hello there Amy";
        var vowels = "aeiou";
        var che = "che";

        return String.Join(" ", (
        from s in sentence.Split(' ')
        let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
        select String.Format("{0}{1}{2}{3}"
        , moveFirstToEnd ? s.Substring(1) : s
        , moveFirstToEnd ? s.Substring(0, 1) : String.Empty
        , che
        , s.Length % 2 == 0 ? "e" : String.Empty
        )
        )
        );
        }





        share|improve this answer























        • It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
          – elgonzo
          Nov 22 at 10:12












        • Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
          – Egbert
          Nov 22 at 14:48











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419325%2fremove-and-add-letters-in-strings%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        I would suggest you to create StringBuilder object and append appropriate string into the IF condition. Try with the below code:



            string Sentence = Console.ReadLine();
        string output = Sentence.Split(' ');
        char letter;
        string che = "che";
        StringBuilder sb = null;
        Console.WriteLine("n");
        string strFinal = "";
        foreach (string s in output)
        {
        letter = s[0];
        sb = new StringBuilder(s);

        if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
        || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
        {
        // Console.WriteLine("first char of the word is a vowel");
        string s1 = sb.Remove(0, 1).ToString();
        sb.Insert(s1.Length, letter);
        sb.Insert(sb.Length, che);
        }
        else
        {
        // Console.WriteLine("first char of a word is a consonant");
        sb.Insert(s.Length, che);
        }

        if (s.Length % 2 == 0)
        {
        // Console.WriteLine("the word has even numbers of letters");
        // sb.Insert(s.Length, "e");
        sb.Insert(sb.Length, "e");
        }
        //Console.WriteLine(firstchar);
        int currentWordLength = s.Length;

        strFinal += sb + " ";
        }

        Console.WriteLine(strFinal);
        Console.ReadKey();





        share|improve this answer























        • I like that. I will try it, as I was just learning StringBuilder.
          – grozdeto
          Nov 22 at 12:04










        • @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
          – Ali Azam
          Nov 22 at 12:15










        • thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
          – grozdeto
          Nov 22 at 12:31












        • @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
          – Ali Azam
          Nov 22 at 13:10










        • @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
          – Ali Azam
          Nov 22 at 13:12















        up vote
        1
        down vote



        accepted










        I would suggest you to create StringBuilder object and append appropriate string into the IF condition. Try with the below code:



            string Sentence = Console.ReadLine();
        string output = Sentence.Split(' ');
        char letter;
        string che = "che";
        StringBuilder sb = null;
        Console.WriteLine("n");
        string strFinal = "";
        foreach (string s in output)
        {
        letter = s[0];
        sb = new StringBuilder(s);

        if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
        || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
        {
        // Console.WriteLine("first char of the word is a vowel");
        string s1 = sb.Remove(0, 1).ToString();
        sb.Insert(s1.Length, letter);
        sb.Insert(sb.Length, che);
        }
        else
        {
        // Console.WriteLine("first char of a word is a consonant");
        sb.Insert(s.Length, che);
        }

        if (s.Length % 2 == 0)
        {
        // Console.WriteLine("the word has even numbers of letters");
        // sb.Insert(s.Length, "e");
        sb.Insert(sb.Length, "e");
        }
        //Console.WriteLine(firstchar);
        int currentWordLength = s.Length;

        strFinal += sb + " ";
        }

        Console.WriteLine(strFinal);
        Console.ReadKey();





        share|improve this answer























        • I like that. I will try it, as I was just learning StringBuilder.
          – grozdeto
          Nov 22 at 12:04










        • @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
          – Ali Azam
          Nov 22 at 12:15










        • thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
          – grozdeto
          Nov 22 at 12:31












        • @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
          – Ali Azam
          Nov 22 at 13:10










        • @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
          – Ali Azam
          Nov 22 at 13:12













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        I would suggest you to create StringBuilder object and append appropriate string into the IF condition. Try with the below code:



            string Sentence = Console.ReadLine();
        string output = Sentence.Split(' ');
        char letter;
        string che = "che";
        StringBuilder sb = null;
        Console.WriteLine("n");
        string strFinal = "";
        foreach (string s in output)
        {
        letter = s[0];
        sb = new StringBuilder(s);

        if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
        || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
        {
        // Console.WriteLine("first char of the word is a vowel");
        string s1 = sb.Remove(0, 1).ToString();
        sb.Insert(s1.Length, letter);
        sb.Insert(sb.Length, che);
        }
        else
        {
        // Console.WriteLine("first char of a word is a consonant");
        sb.Insert(s.Length, che);
        }

        if (s.Length % 2 == 0)
        {
        // Console.WriteLine("the word has even numbers of letters");
        // sb.Insert(s.Length, "e");
        sb.Insert(sb.Length, "e");
        }
        //Console.WriteLine(firstchar);
        int currentWordLength = s.Length;

        strFinal += sb + " ";
        }

        Console.WriteLine(strFinal);
        Console.ReadKey();





        share|improve this answer














        I would suggest you to create StringBuilder object and append appropriate string into the IF condition. Try with the below code:



            string Sentence = Console.ReadLine();
        string output = Sentence.Split(' ');
        char letter;
        string che = "che";
        StringBuilder sb = null;
        Console.WriteLine("n");
        string strFinal = "";
        foreach (string s in output)
        {
        letter = s[0];
        sb = new StringBuilder(s);

        if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
        || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
        {
        // Console.WriteLine("first char of the word is a vowel");
        string s1 = sb.Remove(0, 1).ToString();
        sb.Insert(s1.Length, letter);
        sb.Insert(sb.Length, che);
        }
        else
        {
        // Console.WriteLine("first char of a word is a consonant");
        sb.Insert(s.Length, che);
        }

        if (s.Length % 2 == 0)
        {
        // Console.WriteLine("the word has even numbers of letters");
        // sb.Insert(s.Length, "e");
        sb.Insert(sb.Length, "e");
        }
        //Console.WriteLine(firstchar);
        int currentWordLength = s.Length;

        strFinal += sb + " ";
        }

        Console.WriteLine(strFinal);
        Console.ReadKey();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 13:05

























        answered Nov 21 at 20:33









        Ali Azam

        1,480718




        1,480718












        • I like that. I will try it, as I was just learning StringBuilder.
          – grozdeto
          Nov 22 at 12:04










        • @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
          – Ali Azam
          Nov 22 at 12:15










        • thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
          – grozdeto
          Nov 22 at 12:31












        • @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
          – Ali Azam
          Nov 22 at 13:10










        • @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
          – Ali Azam
          Nov 22 at 13:12


















        • I like that. I will try it, as I was just learning StringBuilder.
          – grozdeto
          Nov 22 at 12:04










        • @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
          – Ali Azam
          Nov 22 at 12:15










        • thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
          – grozdeto
          Nov 22 at 12:31












        • @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
          – Ali Azam
          Nov 22 at 13:10










        • @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
          – Ali Azam
          Nov 22 at 13:12
















        I like that. I will try it, as I was just learning StringBuilder.
        – grozdeto
        Nov 22 at 12:04




        I like that. I will try it, as I was just learning StringBuilder.
        – grozdeto
        Nov 22 at 12:04












        @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
        – Ali Azam
        Nov 22 at 12:15




        @grozdeto I ran and tested at my end. Definitely it will work fine. The output matches your demand. If you get help from this answer, don't forget to accept it please. Thanks.
        – Ali Azam
        Nov 22 at 12:15












        thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
        – grozdeto
        Nov 22 at 12:31






        thank you for your help but it does not work for all the examples. For input: He is no spring chicken anymore The output is: Heeche sieche noeche springeche chickenche nymoreache The correct output should be: Hechee sichee nochee springchee chickenche nymoreache
        – grozdeto
        Nov 22 at 12:31














        @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
        – Ali Azam
        Nov 22 at 13:10




        @grozdeto Hey bro, there is a silly mistake inside the IF (related to even calc) condition. It would be sb.Length instead of s.Length. I have corrected the answer. Please check once again.
        – Ali Azam
        Nov 22 at 13:10












        @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
        – Ali Azam
        Nov 22 at 13:12




        @grozdeto Corrected line should be sb.Insert(sb.Length, "e");
        – Ali Azam
        Nov 22 at 13:12












        up vote
        1
        down vote













        First, I'd recommend changing your translation code to have a function that acts on words, instead of the whole sentence. So the code in foreach (string s in output) should be moved to another function that just acts on that string. And don't try to manipulate the string passed it, create a new one based on the logic you've listed. Once you've created the translated string, return it to the caller. The caller would then reconstruct the sentence from each returned translation.






        share|improve this answer

























          up vote
          1
          down vote













          First, I'd recommend changing your translation code to have a function that acts on words, instead of the whole sentence. So the code in foreach (string s in output) should be moved to another function that just acts on that string. And don't try to manipulate the string passed it, create a new one based on the logic you've listed. Once you've created the translated string, return it to the caller. The caller would then reconstruct the sentence from each returned translation.






          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            First, I'd recommend changing your translation code to have a function that acts on words, instead of the whole sentence. So the code in foreach (string s in output) should be moved to another function that just acts on that string. And don't try to manipulate the string passed it, create a new one based on the logic you've listed. Once you've created the translated string, return it to the caller. The caller would then reconstruct the sentence from each returned translation.






            share|improve this answer












            First, I'd recommend changing your translation code to have a function that acts on words, instead of the whole sentence. So the code in foreach (string s in output) should be moved to another function that just acts on that string. And don't try to manipulate the string passed it, create a new one based on the logic you've listed. Once you've created the translated string, return it to the caller. The caller would then reconstruct the sentence from each returned translation.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 at 19:40









            slolife

            10.8k1968110




            10.8k1968110






















                up vote
                0
                down vote













                You mustn't change your word before the end of ifs. you need to test all of the conditions and save the result in another values, finally do changes on the word:



                foreach (string s in output)
                {
                letter = char.ToLower(s[0]);
                bool isVowel = false;
                bool isEven = false;

                if (letter == 'a' || letter == 'e' || letter == 'i'
                || letter == 'o' || letter == 'u')
                {
                isVowel = true;
                }

                if (s.Length % 2 == 0)
                {
                isEven = true;
                }

                //Now you can change the word
                if (isVowel)
                {
                //Do What you want
                }

                if (isEven)
                {
                //Do What you want
                }

                //Console.WriteLine(firstchar);
                int currentWordLength = s.Length;

                }





                share|improve this answer



























                  up vote
                  0
                  down vote













                  You mustn't change your word before the end of ifs. you need to test all of the conditions and save the result in another values, finally do changes on the word:



                  foreach (string s in output)
                  {
                  letter = char.ToLower(s[0]);
                  bool isVowel = false;
                  bool isEven = false;

                  if (letter == 'a' || letter == 'e' || letter == 'i'
                  || letter == 'o' || letter == 'u')
                  {
                  isVowel = true;
                  }

                  if (s.Length % 2 == 0)
                  {
                  isEven = true;
                  }

                  //Now you can change the word
                  if (isVowel)
                  {
                  //Do What you want
                  }

                  if (isEven)
                  {
                  //Do What you want
                  }

                  //Console.WriteLine(firstchar);
                  int currentWordLength = s.Length;

                  }





                  share|improve this answer

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You mustn't change your word before the end of ifs. you need to test all of the conditions and save the result in another values, finally do changes on the word:



                    foreach (string s in output)
                    {
                    letter = char.ToLower(s[0]);
                    bool isVowel = false;
                    bool isEven = false;

                    if (letter == 'a' || letter == 'e' || letter == 'i'
                    || letter == 'o' || letter == 'u')
                    {
                    isVowel = true;
                    }

                    if (s.Length % 2 == 0)
                    {
                    isEven = true;
                    }

                    //Now you can change the word
                    if (isVowel)
                    {
                    //Do What you want
                    }

                    if (isEven)
                    {
                    //Do What you want
                    }

                    //Console.WriteLine(firstchar);
                    int currentWordLength = s.Length;

                    }





                    share|improve this answer














                    You mustn't change your word before the end of ifs. you need to test all of the conditions and save the result in another values, finally do changes on the word:



                    foreach (string s in output)
                    {
                    letter = char.ToLower(s[0]);
                    bool isVowel = false;
                    bool isEven = false;

                    if (letter == 'a' || letter == 'e' || letter == 'i'
                    || letter == 'o' || letter == 'u')
                    {
                    isVowel = true;
                    }

                    if (s.Length % 2 == 0)
                    {
                    isEven = true;
                    }

                    //Now you can change the word
                    if (isVowel)
                    {
                    //Do What you want
                    }

                    if (isEven)
                    {
                    //Do What you want
                    }

                    //Console.WriteLine(firstchar);
                    int currentWordLength = s.Length;

                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 at 20:19

























                    answered Nov 21 at 19:56









                    Farzin Kanzi

                    2,85721320




                    2,85721320






















                        up vote
                        0
                        down vote













                        Using the obvious extension methods:



                        public static class ExtensionMethods {
                        // ***
                        // *** int Extensions
                        // ***
                        public static bool IsEven(this int n) => n % 2 == 0;

                        // ***
                        // *** String Extensions
                        // ***
                        public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
                        public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
                        }


                        You can use LINQ to process the rules:



                        var vowels = "aeiouAEIOU".ToHashSet();

                        var ans = src.Split(' ')
                        .Select(w => (w.StartsWithOneOf(vowels) ? w.Substring(1)+w[0] : w)+"che"+(w.Length.IsEven() ? "e" : ""))
                        .Join(" ");





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Using the obvious extension methods:



                          public static class ExtensionMethods {
                          // ***
                          // *** int Extensions
                          // ***
                          public static bool IsEven(this int n) => n % 2 == 0;

                          // ***
                          // *** String Extensions
                          // ***
                          public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
                          public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
                          }


                          You can use LINQ to process the rules:



                          var vowels = "aeiouAEIOU".ToHashSet();

                          var ans = src.Split(' ')
                          .Select(w => (w.StartsWithOneOf(vowels) ? w.Substring(1)+w[0] : w)+"che"+(w.Length.IsEven() ? "e" : ""))
                          .Join(" ");





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Using the obvious extension methods:



                            public static class ExtensionMethods {
                            // ***
                            // *** int Extensions
                            // ***
                            public static bool IsEven(this int n) => n % 2 == 0;

                            // ***
                            // *** String Extensions
                            // ***
                            public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
                            public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
                            }


                            You can use LINQ to process the rules:



                            var vowels = "aeiouAEIOU".ToHashSet();

                            var ans = src.Split(' ')
                            .Select(w => (w.StartsWithOneOf(vowels) ? w.Substring(1)+w[0] : w)+"che"+(w.Length.IsEven() ? "e" : ""))
                            .Join(" ");





                            share|improve this answer












                            Using the obvious extension methods:



                            public static class ExtensionMethods {
                            // ***
                            // *** int Extensions
                            // ***
                            public static bool IsEven(this int n) => n % 2 == 0;

                            // ***
                            // *** String Extensions
                            // ***
                            public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
                            public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
                            }


                            You can use LINQ to process the rules:



                            var vowels = "aeiouAEIOU".ToHashSet();

                            var ans = src.Split(' ')
                            .Select(w => (w.StartsWithOneOf(vowels) ? w.Substring(1)+w[0] : w)+"che"+(w.Length.IsEven() ? "e" : ""))
                            .Join(" ");






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 20:35









                            NetMage

                            12.8k11734




                            12.8k11734






















                                up vote
                                0
                                down vote













                                Let's start from splitting the initial problem into smaller ones, with a help of extract methods:



                                  using using System.Text.RegularExpressions;

                                ...

                                private static String ConvertWord(string word) {
                                //TODO: Solution here
                                return word; // <- Stub
                                }

                                private static String ConvertPhrase(string phrase) {
                                // Regex (not Split) to preserve punctuation:
                                // we convert each word (continued sequence of letter A..Z a..z or ')
                                // within the original phrase
                                return Regex.Replace(phrase, @"[A-Za-z']+", match => ConvertWord(match.Value));

                                // Or if there's guarantee, that space is the only separator:
                                // return string.Join(" ", phrase
                                // .Split(new char { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                // .Select(item => ConvertWord(item)));
                                }


                                Now it's time to implement ConvertWord:



                                  private static String ConvertWord(string word) {
                                // Do not forget of special cases - e.g. empty string
                                if (string.IsNullOrEmpty(word))
                                return "chee";

                                // If the word has even number of letters append one more "e" to the end of it.
                                string suffix = word.Length % 2 == 0 ? "chee" : "che";

                                // To cases: starting from vowel / consonant
                                char letter = char.ToUpper(word[0]);

                                if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U')
                                return word.Substring(1) + word.Substring(0, 1) + suffix;
                                else
                                return word + suffix;
                                }


                                Finally



                                  string Sentence = Console.ReadLine();

                                Console.Write(ConvertPhrase(Sentence));


                                For test input



                                "It's just a simple test (demo only): nothing more!"


                                Will get



                                t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!





                                share|improve this answer



















                                • 2




                                  Regex when it’s obviously a beginner’s homework or learning excercise?
                                  – InBetween
                                  Nov 21 at 20:21










                                • @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                  – Dmitry Bychenko
                                  Nov 21 at 20:33






                                • 1




                                  I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                  – InBetween
                                  Nov 21 at 20:57















                                up vote
                                0
                                down vote













                                Let's start from splitting the initial problem into smaller ones, with a help of extract methods:



                                  using using System.Text.RegularExpressions;

                                ...

                                private static String ConvertWord(string word) {
                                //TODO: Solution here
                                return word; // <- Stub
                                }

                                private static String ConvertPhrase(string phrase) {
                                // Regex (not Split) to preserve punctuation:
                                // we convert each word (continued sequence of letter A..Z a..z or ')
                                // within the original phrase
                                return Regex.Replace(phrase, @"[A-Za-z']+", match => ConvertWord(match.Value));

                                // Or if there's guarantee, that space is the only separator:
                                // return string.Join(" ", phrase
                                // .Split(new char { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                // .Select(item => ConvertWord(item)));
                                }


                                Now it's time to implement ConvertWord:



                                  private static String ConvertWord(string word) {
                                // Do not forget of special cases - e.g. empty string
                                if (string.IsNullOrEmpty(word))
                                return "chee";

                                // If the word has even number of letters append one more "e" to the end of it.
                                string suffix = word.Length % 2 == 0 ? "chee" : "che";

                                // To cases: starting from vowel / consonant
                                char letter = char.ToUpper(word[0]);

                                if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U')
                                return word.Substring(1) + word.Substring(0, 1) + suffix;
                                else
                                return word + suffix;
                                }


                                Finally



                                  string Sentence = Console.ReadLine();

                                Console.Write(ConvertPhrase(Sentence));


                                For test input



                                "It's just a simple test (demo only): nothing more!"


                                Will get



                                t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!





                                share|improve this answer



















                                • 2




                                  Regex when it’s obviously a beginner’s homework or learning excercise?
                                  – InBetween
                                  Nov 21 at 20:21










                                • @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                  – Dmitry Bychenko
                                  Nov 21 at 20:33






                                • 1




                                  I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                  – InBetween
                                  Nov 21 at 20:57













                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Let's start from splitting the initial problem into smaller ones, with a help of extract methods:



                                  using using System.Text.RegularExpressions;

                                ...

                                private static String ConvertWord(string word) {
                                //TODO: Solution here
                                return word; // <- Stub
                                }

                                private static String ConvertPhrase(string phrase) {
                                // Regex (not Split) to preserve punctuation:
                                // we convert each word (continued sequence of letter A..Z a..z or ')
                                // within the original phrase
                                return Regex.Replace(phrase, @"[A-Za-z']+", match => ConvertWord(match.Value));

                                // Or if there's guarantee, that space is the only separator:
                                // return string.Join(" ", phrase
                                // .Split(new char { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                // .Select(item => ConvertWord(item)));
                                }


                                Now it's time to implement ConvertWord:



                                  private static String ConvertWord(string word) {
                                // Do not forget of special cases - e.g. empty string
                                if (string.IsNullOrEmpty(word))
                                return "chee";

                                // If the word has even number of letters append one more "e" to the end of it.
                                string suffix = word.Length % 2 == 0 ? "chee" : "che";

                                // To cases: starting from vowel / consonant
                                char letter = char.ToUpper(word[0]);

                                if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U')
                                return word.Substring(1) + word.Substring(0, 1) + suffix;
                                else
                                return word + suffix;
                                }


                                Finally



                                  string Sentence = Console.ReadLine();

                                Console.Write(ConvertPhrase(Sentence));


                                For test input



                                "It's just a simple test (demo only): nothing more!"


                                Will get



                                t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!





                                share|improve this answer














                                Let's start from splitting the initial problem into smaller ones, with a help of extract methods:



                                  using using System.Text.RegularExpressions;

                                ...

                                private static String ConvertWord(string word) {
                                //TODO: Solution here
                                return word; // <- Stub
                                }

                                private static String ConvertPhrase(string phrase) {
                                // Regex (not Split) to preserve punctuation:
                                // we convert each word (continued sequence of letter A..Z a..z or ')
                                // within the original phrase
                                return Regex.Replace(phrase, @"[A-Za-z']+", match => ConvertWord(match.Value));

                                // Or if there's guarantee, that space is the only separator:
                                // return string.Join(" ", phrase
                                // .Split(new char { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                // .Select(item => ConvertWord(item)));
                                }


                                Now it's time to implement ConvertWord:



                                  private static String ConvertWord(string word) {
                                // Do not forget of special cases - e.g. empty string
                                if (string.IsNullOrEmpty(word))
                                return "chee";

                                // If the word has even number of letters append one more "e" to the end of it.
                                string suffix = word.Length % 2 == 0 ? "chee" : "che";

                                // To cases: starting from vowel / consonant
                                char letter = char.ToUpper(word[0]);

                                if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U')
                                return word.Substring(1) + word.Substring(0, 1) + suffix;
                                else
                                return word + suffix;
                                }


                                Finally



                                  string Sentence = Console.ReadLine();

                                Console.Write(ConvertPhrase(Sentence));


                                For test input



                                "It's just a simple test (demo only): nothing more!"


                                Will get



                                t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 22 at 7:34

























                                answered Nov 21 at 19:51









                                Dmitry Bychenko

                                104k992131




                                104k992131








                                • 2




                                  Regex when it’s obviously a beginner’s homework or learning excercise?
                                  – InBetween
                                  Nov 21 at 20:21










                                • @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                  – Dmitry Bychenko
                                  Nov 21 at 20:33






                                • 1




                                  I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                  – InBetween
                                  Nov 21 at 20:57














                                • 2




                                  Regex when it’s obviously a beginner’s homework or learning excercise?
                                  – InBetween
                                  Nov 21 at 20:21










                                • @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                  – Dmitry Bychenko
                                  Nov 21 at 20:33






                                • 1




                                  I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                  – InBetween
                                  Nov 21 at 20:57








                                2




                                2




                                Regex when it’s obviously a beginner’s homework or learning excercise?
                                – InBetween
                                Nov 21 at 20:21




                                Regex when it’s obviously a beginner’s homework or learning excercise?
                                – InBetween
                                Nov 21 at 20:21












                                @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                – Dmitry Bychenko
                                Nov 21 at 20:33




                                @InBetween: I'm sorry for Regex (which is quite simple) but it seems to be the easiest way to extract word in case punctuation is present in the sentence: Split will get (demo not demo. Some homeworks are in fact more difficult than they are supposed to be.
                                – Dmitry Bychenko
                                Nov 21 at 20:33




                                1




                                1




                                I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                – InBetween
                                Nov 21 at 20:57




                                I'd be more inclined to think you are making it harder than it needs to be. There is absolutely no requirement in OP about punctuation nor does it seem to be an expected or considered input given the level of the assignment.
                                – InBetween
                                Nov 21 at 20:57










                                up vote
                                -1
                                down vote













                                I would say break the logic into specific units so you can write tests,



                                    static void Main(string args)
                                {
                                var input = Console.ReadLine();
                                var inputs = input.Split(' ');
                                var sentence = string.Join(" ", inputs.Select(ConvertWord));
                                Console.Write(sentence);
                                Console.Read();
                                }

                                internal static string ConvertWord(string input)
                                {
                                const string che = "che";

                                var vowels = new List<string>
                                {
                                "a","A", "e", "E", "i", "I", "o", "O", "u", "U"
                                };

                                var firstChar = input.First();

                                var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));

                                string rule2String, output;

                                if (string.IsNullOrEmpty(startsWithVowel))
                                {
                                output = input + che;
                                }

                                else
                                {
                                output = input.Substring(1) + startsWithVowel + che;
                                }

                                rule2String = IsLengthEven(input)
                                ? output + "e"
                                : output
                                ;

                                return rule2String;
                                }

                                internal static bool IsLengthEven(string input)
                                {
                                return input.Length % 2 == 0;
                                }


                                Hope this Helps!
                                PS: I have not covered the edge cases






                                share|improve this answer





















                                • In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                  – elgonzo
                                  Nov 22 at 10:06

















                                up vote
                                -1
                                down vote













                                I would say break the logic into specific units so you can write tests,



                                    static void Main(string args)
                                {
                                var input = Console.ReadLine();
                                var inputs = input.Split(' ');
                                var sentence = string.Join(" ", inputs.Select(ConvertWord));
                                Console.Write(sentence);
                                Console.Read();
                                }

                                internal static string ConvertWord(string input)
                                {
                                const string che = "che";

                                var vowels = new List<string>
                                {
                                "a","A", "e", "E", "i", "I", "o", "O", "u", "U"
                                };

                                var firstChar = input.First();

                                var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));

                                string rule2String, output;

                                if (string.IsNullOrEmpty(startsWithVowel))
                                {
                                output = input + che;
                                }

                                else
                                {
                                output = input.Substring(1) + startsWithVowel + che;
                                }

                                rule2String = IsLengthEven(input)
                                ? output + "e"
                                : output
                                ;

                                return rule2String;
                                }

                                internal static bool IsLengthEven(string input)
                                {
                                return input.Length % 2 == 0;
                                }


                                Hope this Helps!
                                PS: I have not covered the edge cases






                                share|improve this answer





















                                • In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                  – elgonzo
                                  Nov 22 at 10:06















                                up vote
                                -1
                                down vote










                                up vote
                                -1
                                down vote









                                I would say break the logic into specific units so you can write tests,



                                    static void Main(string args)
                                {
                                var input = Console.ReadLine();
                                var inputs = input.Split(' ');
                                var sentence = string.Join(" ", inputs.Select(ConvertWord));
                                Console.Write(sentence);
                                Console.Read();
                                }

                                internal static string ConvertWord(string input)
                                {
                                const string che = "che";

                                var vowels = new List<string>
                                {
                                "a","A", "e", "E", "i", "I", "o", "O", "u", "U"
                                };

                                var firstChar = input.First();

                                var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));

                                string rule2String, output;

                                if (string.IsNullOrEmpty(startsWithVowel))
                                {
                                output = input + che;
                                }

                                else
                                {
                                output = input.Substring(1) + startsWithVowel + che;
                                }

                                rule2String = IsLengthEven(input)
                                ? output + "e"
                                : output
                                ;

                                return rule2String;
                                }

                                internal static bool IsLengthEven(string input)
                                {
                                return input.Length % 2 == 0;
                                }


                                Hope this Helps!
                                PS: I have not covered the edge cases






                                share|improve this answer












                                I would say break the logic into specific units so you can write tests,



                                    static void Main(string args)
                                {
                                var input = Console.ReadLine();
                                var inputs = input.Split(' ');
                                var sentence = string.Join(" ", inputs.Select(ConvertWord));
                                Console.Write(sentence);
                                Console.Read();
                                }

                                internal static string ConvertWord(string input)
                                {
                                const string che = "che";

                                var vowels = new List<string>
                                {
                                "a","A", "e", "E", "i", "I", "o", "O", "u", "U"
                                };

                                var firstChar = input.First();

                                var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));

                                string rule2String, output;

                                if (string.IsNullOrEmpty(startsWithVowel))
                                {
                                output = input + che;
                                }

                                else
                                {
                                output = input.Substring(1) + startsWithVowel + che;
                                }

                                rule2String = IsLengthEven(input)
                                ? output + "e"
                                : output
                                ;

                                return rule2String;
                                }

                                internal static bool IsLengthEven(string input)
                                {
                                return input.Length % 2 == 0;
                                }


                                Hope this Helps!
                                PS: I have not covered the edge cases







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 21 at 20:17









                                HarnishRShah

                                1




                                1












                                • In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                  – elgonzo
                                  Nov 22 at 10:06




















                                • In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                  – elgonzo
                                  Nov 22 at 10:06


















                                In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                – elgonzo
                                Nov 22 at 10:06






                                In your code, startsWithVowel will always be null. Also, why are you making the vowel test so needlessly over-complicated? Why would you not want to use a simple char array/list in conjunction with the Contains method...?
                                – elgonzo
                                Nov 22 at 10:06












                                up vote
                                -1
                                down vote













                                I would recommend to use String.Join using Linq with String.Format. Then the solution is quite easy:



                                    private String convertSentence() {
                                var sentence = "Hello there Amy";
                                var vowels = "aeiou";
                                var che = "che";

                                return String.Join(" ", (
                                from s in sentence.Split(' ')
                                let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
                                select String.Format("{0}{1}{2}{3}"
                                , moveFirstToEnd ? s.Substring(1) : s
                                , moveFirstToEnd ? s.Substring(0, 1) : String.Empty
                                , che
                                , s.Length % 2 == 0 ? "e" : String.Empty
                                )
                                )
                                );
                                }





                                share|improve this answer























                                • It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                  – elgonzo
                                  Nov 22 at 10:12












                                • Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                  – Egbert
                                  Nov 22 at 14:48















                                up vote
                                -1
                                down vote













                                I would recommend to use String.Join using Linq with String.Format. Then the solution is quite easy:



                                    private String convertSentence() {
                                var sentence = "Hello there Amy";
                                var vowels = "aeiou";
                                var che = "che";

                                return String.Join(" ", (
                                from s in sentence.Split(' ')
                                let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
                                select String.Format("{0}{1}{2}{3}"
                                , moveFirstToEnd ? s.Substring(1) : s
                                , moveFirstToEnd ? s.Substring(0, 1) : String.Empty
                                , che
                                , s.Length % 2 == 0 ? "e" : String.Empty
                                )
                                )
                                );
                                }





                                share|improve this answer























                                • It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                  – elgonzo
                                  Nov 22 at 10:12












                                • Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                  – Egbert
                                  Nov 22 at 14:48













                                up vote
                                -1
                                down vote










                                up vote
                                -1
                                down vote









                                I would recommend to use String.Join using Linq with String.Format. Then the solution is quite easy:



                                    private String convertSentence() {
                                var sentence = "Hello there Amy";
                                var vowels = "aeiou";
                                var che = "che";

                                return String.Join(" ", (
                                from s in sentence.Split(' ')
                                let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
                                select String.Format("{0}{1}{2}{3}"
                                , moveFirstToEnd ? s.Substring(1) : s
                                , moveFirstToEnd ? s.Substring(0, 1) : String.Empty
                                , che
                                , s.Length % 2 == 0 ? "e" : String.Empty
                                )
                                )
                                );
                                }





                                share|improve this answer














                                I would recommend to use String.Join using Linq with String.Format. Then the solution is quite easy:



                                    private String convertSentence() {
                                var sentence = "Hello there Amy";
                                var vowels = "aeiou";
                                var che = "che";

                                return String.Join(" ", (
                                from s in sentence.Split(' ')
                                let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
                                select String.Format("{0}{1}{2}{3}"
                                , moveFirstToEnd ? s.Substring(1) : s
                                , moveFirstToEnd ? s.Substring(0, 1) : String.Empty
                                , che
                                , s.Length % 2 == 0 ? "e" : String.Empty
                                )
                                )
                                );
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 at 22:18

























                                answered Nov 21 at 20:49









                                Egbert

                                10117




                                10117












                                • It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                  – elgonzo
                                  Nov 22 at 10:12












                                • Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                  – Egbert
                                  Nov 22 at 14:48


















                                • It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                  – elgonzo
                                  Nov 22 at 10:12












                                • Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                  – Egbert
                                  Nov 22 at 14:48
















                                It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                – elgonzo
                                Nov 22 at 10:12






                                It is rather clear from the question that it is some school homework/assignment or coursework to learn basics about programming. While i believe your answer works (in a technical sense), it completely misses the mark by using a comparatively complex Linq query that is lightyears beyond the scope of the homework assignment asked about.
                                – elgonzo
                                Nov 22 at 10:12














                                Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                – Egbert
                                Nov 22 at 14:48




                                Why to downvote a good answer? Even if it is homework, other people use Stackoverflow too to find answers. Not only the one who asked this question looks at this page.
                                – Egbert
                                Nov 22 at 14:48


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419325%2fremove-and-add-letters-in-strings%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)