-2

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();
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98
CrazyIvan
  • 31
  • 1
  • 2
  • 5
    The magic is happening in the `LongerWord` method, so you're going to have to include that code as well. – Abion47 Feb 05 '17 at 22:19
  • 2
    Please note that you need to know what you're measuring here. A naive implementation would compare the `.Length` property of the two strings, but since you've clearly specified the longer *word*, this may not actually be correct, depending on your definition of "length of a word". `string.Length` returns the number of *characters* which translates to unicode code points, and a *visible character* may not translate to only one such code point, the character `à` may be encoded both as a single character and two, the letter `a` and the diacritic "`". – Lasse V. Karlsen Feb 05 '17 at 22:25
  • As an example, consider the string `string s = "a\u0300a\u0300a\u0300";`, is this string longer or shorter than the word `"word"` ? (note that the first string will display as `ààà`, but have a length of 6). – Lasse V. Karlsen Feb 05 '17 at 22:28
  • 2
    I am also going to point out that judging by what kind or program you're making I'm pretty sure this is homework or some other school-related task, in which case everything I said above is something you can file under "nice to know" but most likely completely something you can completely disregard. Still, if this *is* school work, and something you need to turn in, a note about the above thing might even be worth extra credit if you can get it right. – Lasse V. Karlsen Feb 05 '17 at 22:31

3 Answers3

6

that should be a start...of something...

private string LongerString(string x, string y)
{
    return x.Length > y.Length ? x : y;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
ymz
  • 6,602
  • 1
  • 20
  • 39
  • 1
    I doubt the OP will understand when he has trouble solving this with an `if`. – Filburt Feb 05 '17 at 22:27
  • 1
    @trailmax You should rather downvote the OP if you think it's homework and doesn't show effort. – Filburt Feb 05 '17 at 22:55
  • @Filburt no point down-voting a 1-point user. I voted to close the question. – trailmax Feb 05 '17 at 22:56
  • @Filburt.. thanks for the backup.. In person - I think that any question that **may** assist others is worthy enough to answer. – ymz Feb 05 '17 at 22:57
  • @trailmax It counts towards a future question ban preventing further homework dumps. Downvoting answers doesn't count against the OP. – Filburt Feb 05 '17 at 22:58
  • @Filburt but downvoting an answer counts for answer ban for providing code-only answer... So somewhat useful too, but indeed "this is answer to homework question" is not recommended reason to downvote (one still free to do so, just not in spirit of SO). – Alexei Levenkov Feb 05 '17 at 23:02
  • 2
    @trailmax Judging from the question, it seems like OP did his due diligence in trying to complete the assignment himself before coming to SO. As such, the question is perfectly acceptable. – Abion47 Feb 06 '17 at 07:05
2

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:

public static string LongerString(string word1 , string word2)
        {
            if (word1.Length >= word2.Length)
            {
                return word1;
            }
            else
            {
                return word2;
            }
        }
CrazyIvan
  • 31
  • 1
  • 2
0

I don't see why using stringName.Length won't work. Look at this code:

Console.Write("Give 1. word >");
string word1 = Console.ReadLine();
Console.Write("Give 2. word >");
string word2 = Console.ReadLine();

if(word1.Length > word2.Length)
{
    Console.Write("String One is longer.");
}
else
{
    Console.Write("String Two is longer.");
}

As a function, it would like this:

namespace String_Length
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Give 1. word >");
            string word1 = Console.ReadLine();
            Console.Write("Give 2. word >");
            string word2 = Console.ReadLine();

            CompareStrings(word1, word2); 
            Console.ReadKey();
        }

        static void CompareStrings(string one, string two)
        {
            if (one.Length > two.Length)
            {
                Console.Write("String One is longer.");
            }
            else
            {
                Console.Write("String Two is longer.");
            }
        }
    }
}

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.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
  • @trailmax How do you know that it is homework. The person asked a question, it is our job to answer that question, not to judge whether we should or shouldn't. Besides, even if it was a homework assignment, at least he has learnt something. It's better than him not learning anything. By the way, I know how CrazyIvan feels when you try and try again to no avail to get code to work. I am 14, and have faced many similar a problem. –  Feb 05 '17 at 23:03
  • @trailmax The homework issue is not whether the question is about homework. It's about whether the asker has given the task a reasonable try himself before asking for help as opposed to just posting the homework question and demanding the answer. Judging from the question, it seems apparent that OP has indeed tried to do this himself and is merely asking for advice rather than free labor. – Abion47 Feb 05 '17 at 23:19
  • Thank you @JamieCorkhill and others! Yes, it was one of my homework tasks that I had problems with. I used almost the same method as Jamie suggested, but I had few errors. In the end I got full points. I had to use return statement to get it working properly. I was hesitant about posting this question, because it was my "homework", but I really wanted to get the function working. – CrazyIvan Feb 06 '17 at 10:29