Write a C# program using a Linq statement. Create a string list. Use Console.Rea
ID: 3811518 • Letter: W
Question
Write a C# program using a Linq statement.
Create a string list.
Use Console.ReadLine() to collect values of firstName, lastName, street, city, state, zip, save them to List.
Write a simple Linq statement, then call method ***UppercaseWords() to change first letter to uppercase.
Create a foreach statement to display the information (foreach (var in )).
***Method uppercaseWords() is provided and needs to be included entirely in the program:
public static string UppercaseWords(string value)
{
char[] array = value.ToCharArray();
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
_______________________________________________OUTPUT____________________________________________________
(Note: The following information is provided by the user.)
Enter first name: michael
Enter last name: johnson
Enter street: 999 first street
Enter city: seattle
Enter state: washington
Enter Sip code: 98433
(The following information is generated automatically. There are upper case letters at the beginning of every word.)
Costumer’s information:
Michael
Johnson
999 First Street
Seattle
Washington
98433
Press any key to continue...
______________________________________________________________________________________________________________________________
I have also included a similar sample code that can be used as a reference for this project:
// Fig. 9.7: LINQWithListCollection.cs
// LINQ to Objects using a List< string >.
using System;
using System.Linq;
using System.Collections.Generic;
public class LINQWithListCollection
{
public static void Main( string[] args )
{
// populate a List of strings
List< string > items = new List< string >();
items.Add( "aQua" ); // add "aQua" to the end of the List
items.Add( "RusT" ); // add "RusT" to the end of the List
items.Add( "yElLow" ); // add "yElLow" to the end of the List
items.Add( "rEd" ); // add "rEd" to the end of the List
// convert all strings to uppercase; select those starting with "R"
var startsWithR =
from item in items
let uppercaseString = item.ToUpper()
where uppercaseString.StartsWith( "R" )
orderby uppercaseString
select uppercaseString;
// display query results
foreach ( var item in startsWithR )
Console.Write( "{0} ", item );
Console.WriteLine(); // output end of line
items.Add( "rUbY" ); // add "rUbY" to the end of the List
items.Add( "SaFfRon" ); // add "SaFfRon" to the end of the List
// display updated query results
foreach ( var item in startsWithR )
Console.Write( "{0} ", item );
Console.WriteLine(); // output end of line
} // end Main
} // end class LINQWithListCollection
Explanation / Answer
using System;
using System.Linq;
using System.Collections.Generic;
public class LINQWithListCollection
{
public static string UppercaseWords(string value)
{
char[] array = value.ToCharArray();
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
public static void Main( string[] args )
{
List< string > items = new List< string >();
Console.Write("Enter first name: " );
String input = Console.ReadLine();
items.Add(input);
Console.Write("Enter last name: " );
input = Console.ReadLine();
items.Add(input);
Console.Write("Enter street: " );
input = Console.ReadLine();
items.Add(input);
Console.Write("Enter city: " );
input = Console.ReadLine();
items.Add(input);
Console.WriteEnter state: " );
input = Console.ReadLine();
items.Add(input);
Console.Write("Enter Zip code: " );
input = Console.ReadLine();
items.Add(input);
var allCapital =
from item in items
let uppercaseString = UppercaseWords(item)
select uppercaseString;
foreach ( var item in allCapital ){
Console.WriteLine( "{0} ", item );
}
Console.WriteLine("Press any key to continue...");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.