In this C# program, I need to show the information provided by the user. I have
ID: 3814218 • Letter: I
Question
In this C# program, I need to show the information provided by the user. I have attached the code but I need help generating the output. The output should look like this:
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 zip 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...
Here is the code I have:
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.Write("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...");
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
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.Write("Enter 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;
Console.WriteLine();
foreach (var item in allCapital)
{
Console.WriteLine("{0} ", item);
}
Console.WriteLine("Press any key to continue...");
}
}
Output:
Enter first name : micheal Enter last name : johnson Enter street : 999 first street Enter city : seattle Enter state : washington Enter Zip code : 98433
Michael
Johnson
999 First Street
Seattle
Washington
98433
Press any key to continue...