1

I'm new to programming in c# and I'm trying to figure out how I could potentially reverse all words except words containing e in a string.

my current code will detect words containing e, and just writes them down in another textbox:

string text = txbInput.Text;
            var words = text.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Contains('e'))
                {
                   txbOutput.Text += words[i];
                }

Current:

Input: chicken crossing the road

Output: chickenthe

.

Expected outcome:

Input: chicken crossing the road

Output chicken gnissorc the daor

5
  • 2
    Possible duplicate of To reverse a string. Commented May 9, 2019 at 22:01
  • I'm not trying to make my whole string reversed. Commented May 9, 2019 at 22:06
  • @DennisTanahatoe you're not, but that question shows how you can do it with a string, so just pass to the method there the strings that do not have an e in it?!?! Commented May 9, 2019 at 22:07
  • 1
    Write an else case that reverses the word..? Commented May 9, 2019 at 22:11
  • The logic behind combing those 2 things is the struggle.. Commented May 9, 2019 at 22:21

3 Answers 3

3

You can simply split the word on the space character, then, for each word, select either the word itself, or the word reversed (depending on whether or not it contains the 'e' character), and then join them back together again with the space character:

txbOutput.Text = string.Join(" ", txbInput.Text.Split(' ')
    .Select(word => word.Contains("e") ? string.Concat(word.Reverse()) : word));
Sign up to request clarification or add additional context in comments.

2 Comments

Superb. Neat & elegant!
1

Outputs: chicken gnissorc the daor

using System;


namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "chicken crossing the road";

            foreach (var item in input.Split(' '))
            {
                if (item.Contains('e'))
                {
                    Console.Write(item + ' ');
                }
                else
                {
                    Console.Write(Reverse(item) + ' ');
                }
            }
        }

        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }
}
enter code here

EDIT

 foreach (var item in input.Split(' '))
    {
        if (item.Contains('e'))
        {
            txbOutput.Text = txbOutput.Text+ item + ' ';
        }
        else
        {
            txbOutput.Text= txbOutput.Text+ Reverse(item) + ' ';
        }
    }

3 Comments

Array.Reverse(s.ToCharArray()) does not reverse strings.
The question starts out with "I am new to programming..". I think it is important to work with simple and understandable answers to introduce core concepts.
What could I potentially add, in order to ignore the special characters?
0

You can try using the following code -

string.Join(” “,
      str.Split(‘ ‘)
         .Select(x => new String(x.Reverse().ToArray()))
         .ToArray());

Copied from - https://www.declarecode.com/code-solutions/csharp/caprogramtoreverseeachwordinthegivenstring

3 Comments

Are you affiliated with the Declare Code blog?
@JeremyCaney no why?
Link in answer is dead - "DNS_PROBE_FINISHED_NXDOMAIN".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.