using System;
using System.Collections.Generic;
/*
This program extracts all integer values from a mixed string
and sorts them using the language's built‑in list sorting mechanism.
It demonstrates:
- clear separation of concerns using methods
- straightforward character parsing
- dynamic storage using List<int>
- efficient sorting with List.Sort
*/
class ExtractAndSortNumbers
{
// ------------------------------------------------------------
// Extract all integer values from a mixed string.
// The method walks through each character, collects digits,
// and converts completed digit sequences into integers.
// ------------------------------------------------------------
static List<int> ExtractNumbers(string input)
{
var numbers = new List<int>();
var buffer = new System.Text.StringBuilder();
foreach (char ch in input) {
if (char.IsDigit(ch)) {
// accumulate digits
buffer.Append(ch);
}
else
{
// flush buffer if it contains a number
if (buffer.Length > 0) {
numbers.Add(int.Parse(buffer.ToString()));
buffer.Clear();
}
}
}
// flush trailing number
if (buffer.Length > 0) {
numbers.Add(int.Parse(buffer.ToString()));
}
return numbers;
}
// ------------------------------------------------------------
// Print all numbers in a space‑separated format.
// ------------------------------------------------------------
static void PrintNumbers(List<int> numbers)
{
for (int i = 0; i < numbers.Count; i++) {
Console.Write(numbers[i]);
if (i < numbers.Count - 1) {
Console.Write(" ");
}
}
Console.WriteLine();
}
// ------------------------------------------------------------
// Main
// ------------------------------------------------------------
static void Main()
{
string input = "1000withz7 and3 or 99 give42";
// extract numbers
List<int> numbers = ExtractNumbers(input);
// sort numbers
numbers.Sort();
// display result
Console.Write("Sorted numbers: ");
PrintNumbers(numbers);
}
}
/*
run:
Sorted numbers: 3 7 42 99 1000
*/