Basic Programming C# Examples

C# Program to Find the longest word in String

The Program in C# Program to Find the longest word in String is given below:

using System;

namespace LongestWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter a string here to find the longest word: ");
            string inputString = Console.ReadLine();
            string[] words = inputString.Split(' ');
            string longestWord = "";

            foreach (string word in words)
            {
                if (word.Length > longestWord.Length)
                {
                    longestWord = word;
                }
            }

            Console.WriteLine("Okay, the longest word in the string is: " + longestWord);
        }
    }
}

Output:


Hello Codeauri Family, enter a string here to find the longest word:

keep coding with codeauri
Okay, the longest word in the string is: codeauri

Pro-Tips💡

Here are the step by step execution of above program are:

  1. The first line “using System;” is a directive that brings the System namespace into scope and makes its classes and methods available to the code.
  2. The namespace declaration “namespace LongestWord” creates a new namespace with the name “LongestWord”.
  3. The class declaration “class Program” creates a new class with the name “Program”.
  4. The “Main” method is the entry point of the application. It prints a message to the console asking the user to enter a string and then reads the input string from the console using Console.ReadLine().
  5. The code splits the input string into individual words using the string method “Split” and stores the words in an array “words”.
  6. The code declares a string variable “longestWord” and initializes it to an empty string.
  7. The code uses a “foreach” loop to iterate over the words in the “words” array. For each word, the code checks if its length is greater than the length of the “longestWord”. If it is, the code sets “longestWord” to the current word.
  8. After the loop finishes, the code writes the “longestWord” to the console as the result.
Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X