A practical example that iterates through all words in a sentence to find the one with the greatest length.
ID: 824
See: Introduction, Length = [int]
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
t.Text = "Find the longest word in this sentence.";
t.Pattern("{@Alpha}"); // Match all words
t.Find();
int maxLength = 0;
string longestWord = "";
foreach(var match in t.Matches) {
if (match.Length > maxLength) {
maxLength = match.Length;
longestWord = match.Text;
}
}
Console.WriteLine($"Longest word is '{longestWord}' with length: {maxLength}");
Longest word is 'sentence' with length: 8 using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); t.Text = "Find the longest word in this sentence."; t.Pattern("{@Alpha}"); // Match all words t.Find(); int maxLength = 0; string longestWord = ""; foreach(var match in t.Matches) { if (match.Length > maxLength) { maxLength = match.Length; longestWord = match.Text; } } Console.WriteLine($"Longest word is '{longestWord}' with length: {maxLength}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
t.Text("Find the longest word in this sentence.");
t.Pattern("{@Alpha}"); // Match all words
t.Find();
int maxLength = 0;
string longestWord = "";
for(auto match : t.Matches()) {
if (match.Length() > maxLength) {
maxLength = match.Length();
longestWord = match.Text();
}
}
cout << "Longest word is '" << longestWord << "' with length: " << maxLength << endl;
}
Longest word is 'sentence' with length: 8 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.Text("Find the longest word in this sentence."); t.Pattern("{@Alpha}"); // Match all words t.Find(); int maxLength = 0; string longestWord = ""; for(auto match : t.Matches()) { if (match.Length() > maxLength) { maxLength = match.Length(); longestWord = match.Text(); } } cout << "Longest word is '" << longestWord << "' with length: " << maxLength << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
t.Text = "Find the longest word in this sentence."
t.Pattern("{@Alpha}") '// Match all words
t.Find()
Dim maxLength As Integer = 0
Dim longestWord As String = ""
For Each match In t.Matches
If match.Length > maxLength Then
maxLength = match.Length
longestWord = match.Text
End If
Next
Console.WriteLine($"Longest word is '{longestWord}' with length: {maxLength}")
End Sub
End Module
Longest word is 'sentence' with length: 8 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() t.Text = "Find the longest word in this sentence." t.Pattern("{@Alpha}") '// Match all words t.Find() Dim maxLength As Integer = 0 Dim longestWord As String = "" For Each match In t.Matches If match.Length > maxLength Then maxLength = match.Length longestWord = match.Text End If Next Console.WriteLine($"Longest word is '{longestWord}' with length: {maxLength}") End Sub End Module