using System;
public class RemoveLastWordProgram
{
// Removes the last word from a space-separated string.
public static string RemoveLastWord(string input)
{
// Split into words, ignoring extra spaces
string[] arr = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// No words → return empty string
if (arr.Length == 0)
return "";
// One word → return original input unchanged
if (arr.Length == 1)
return arr[0];
// Remove the last word
Array.Resize(ref arr, arr.Length - 1);
return string.Join(" ", arr);
}
public static void Main(string[] args)
{
string s = "c# vb.net c c++ java";
string result = RemoveLastWord(s);
Console.WriteLine("1. " + result);
result = RemoveLastWord("");
Console.WriteLine("2. " + result);
result = RemoveLastWord("c#");
Console.WriteLine("3. " + result);
result = RemoveLastWord("c c++ java ");
Console.WriteLine("4. " + result);
result = RemoveLastWord(" ");
Console.WriteLine("5. " + result);
}
}
/*
run:
1. c# vb.net c c++
2.
3. c#
*/