I will demonstrate how to reverse a string in a simple way and how to find a specfic word in the given string using C#. Here is the code sample that shows you how to reverse a string in C# and find the desired word in a given string.
Reverse a string in simple steps using C#.
  1. public string ReverseString(string input)
  2. {
  3. if (string.IsNullOrEmpty(input))
  4. {
  5. throw new ArgumentNullException();
  6. }
  7. var output = input.Reverse();
  8. return new string(output.ToArray());
  9. }
Find a word in given string. We assume the separator of word is space (' ');
  1. /// Example 1: GetWordFromText("one two three", 2) should return "two"
  2. /// Example 2: GetWordFromText("one;two three", 2) should return "three"
  3. /// Example 3: GetWordFromText("one", 1) should return "one"
  4. /// When input parameter wordNumberToFind is less than 1, method should throw
  5. /// ArgumentOutOfRangeException. When input text does not have enough words
  6. /// (GetWordFromText("one", 2)), method should throw ArgumentException.
  7. /// When input is null method should throw `ArgumentNullException`.
  8. /// Method should ignore all spaces in the beginning and in the end of input text.
  9. /// </returns>
  10. public string GetWordFromText(string input, int wordNumberToFind)
  11. {
  12. // TODO: Implement logic HERE
  13. if (wordNumberToFind < 1)
  14. {
  15. throw new ArgumentOutOfRangeException();
  16. }
  17. if (string.IsNullOrWhiteSpace(input))
  18. {
  19. throw new ArgumentNullException();
  20. }
  21. var words = input.Split(' ');
  22. if (wordNumberToFind > words.Length)
  23. {
  24. throw new ArgumentException();
  25. }
  26. while (string.IsNullOrWhiteSpace(words[wordNumberToFind - 1]))
  27. {
  28. wordNumberToFind++;
  29. }
  30. return words[wordNumberToFind - 1];
  31. }
Let's have some unit tests to verify the above code.
  1. namespace TaskLib.Tests
  2. {
  3. using System;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6. [TestFixture]
  7. public class StringTests
  8. {
  9. [Test]
  10. public void When_WordToFindIsNotInTheInput_Then_ArgumentExceptionIsThrown()
  11. {
  12. // Arrange
  13. var tested = new StringHelpers();
  14. // Act
  15. Assert.Throws<ArgumentException>(() => { var word = tested.GetWordFromText("two words", 3); });
  16. }
  17. [Test]
  18. public void When_InputTextContainsFewWords_Then_ProperOneIsReturned()
  19. {
  20. // Arrange
  21. var tested = new StringHelpers();
  22. // Act
  23. var word = tested.GetWordFromText("one;two three", 2);
  24. // Assert
  25. word.Should().Be("three");
  26. }
  27. [Test]
  28. public void When_InputTextIsSymmetrical_Then_ItIsReturned()
  29. {
  30. // Arrange
  31. var tested = new StringHelpers();
  32. // Act
  33. var reversed = tested.Reverse("evil live");
  34. // Assert
  35. reversed.Should().Be("evil live");
  36. }
  37. [Test]
  38. public void When_InputTextIsPassed_Then_ItIsReversed()
  39. {
  40. // Arrange
  41. var tested = new StringHelpers();
  42. // Act
  43. var reversed = tested.Reverse("abcd efgh");
  44. // Assert
  45. reversed.Should().Be("hgfe dcba");
  46. }
  47. }
  48. }
Thanks. I hope you liked this simple approach of reversing a string and finding a word in the string.