-3

I have a problem with C#

enter image description here enter image description here this is what i am gonna do I try to covert string to int, it still no active.

THIS IS MY FULL CODE!

using System;

namespace New
{
    class Program
    {
        public static string lastWord(string []a)
        {

            string word = a[0];

            foreach (string s in a)
                if (s > word) //ERROR
                {
                    word = s;
                }
            return word;
        }
        public static void Main(string[] args)
        {

            Console.WriteLine("Please enter 3 words, separated by a space: ");
            string input = Console.ReadLine();
            string[] nameArr = input.Split(" ");
            string name = lastWord(nameArr);
            Console.WriteLine("The word that comes last is " + name);
        }
    }
}
Ciaran McHale
  • 2,126
  • 14
  • 21
Hào
  • 3
  • 2
  • Welcome to StackOverflow. Please take a [tour] and see [ask]. Specifically you should post a [mre] with all relevant errors as plain text. – wohlstad Mar 20 '23 at 17:47
  • 1
    The exception seems to indicate the input string from variable `s` or `word` is not a valid number. Please look at the current value of each variable to tell us more. Also make sure to specify a `CultureInfo` instance so language-specific symbols (like in "1 234,6") will be recognized. Look at [this answer](https://stackoverflow.com/a/47323966) for more info. – SandRock Mar 20 '23 at 17:53
  • How can i make it active – Hào Mar 20 '23 at 18:04
  • the error screen capture does not match the code you show here. – pm100 Mar 20 '23 at 18:30

1 Answers1

1

The compile error is telling you that you can't use the < operator with operands of type string.

In your case, since you're trying to find the last word in an array, you don't need to compare strings, you just need to get the last item from the array.

One way would be to walk through the array in a loop and re-assign the word for each item until you get to the end, and then return that item:

public static string LastWord(string[] input)
{
    // Handle some error cases as you see fit
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // Set word equal to the first string
    string word = input[0];

    // Loop through each item in the array
    for(int i = 1; i < input.Length; i++)
    {
        // Reset word to the current item
        word = input[i];
    }

    // Now word is the last item, so we can return it
    return word;
}

However, a faster way would be to use the .Length property of the array to determine the last index, and return the word at that index:

public static string LastWord(string[] input)
{
    // Handle some error cases as you see fit
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // Return the last string in the input array using 
    // the array's length to determine the last index
    return input[input.Length - 1];
}

And of course there's always Linq:

return input?.LastOrDefault();
Rufus L
  • 36,127
  • 5
  • 30
  • 43