I need to make function that determine longer word of two entered. I have tried to use if-statement and String.Length
, but I can't get it right. What would be the best way to make the function?
Below is the main program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class LongerWord
{
public static void Main(string[] args)
{
Console.Write("Give 1. word >");
String word1 = Console.ReadLine();
Console.Write("Give 2. word >");
String word2 = Console.ReadLine();
String longer = LongerString(word1, word2);
Console.WriteLine("\"" + longer + "\" is longer word");
Console.ReadKey();
}
}
So I figured out how to do the function properly, thanks for all your help! I had to use return statement. If words were the same length then first word needed to be the displayed one. Here is what I got:
that should be a start...of something...
I don't see why using
stringName.Length
won't work. Look at this code:As a function, it would like this:
You might also want to add code for if the length of both strings are equal too each other, possibly with a try-catch block. I hope this helps.