Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 14:27
Show Gist options
  • Save ChrisMoney/0c7400713e68e184ad37 to your computer and use it in GitHub Desktop.
Save ChrisMoney/0c7400713e68e184ad37 to your computer and use it in GitHub Desktop.
C# --Builds A Sentence
// Build A Sentence – the following program constructs sentences by concatenating user input until the //user enters one of the termination characters
//This program shows you when you need to look for string equality the
using System;
namespace BuildASentence
{
public class Program
{
public static void Main(string[] args)
Console.WriteLine(“Each line you enter will be added to a sentence until you enter Exit or Quit”);
// ask the user for input; continue concatenating the phrases input until the user enters exit or quit (start with a null sentence)
string sSentence = “ “;
for(; ;)
{
// get the next line
Console.WriteLine(“Enter a string”);
string sLine = Console.ReadLine();
// exit the loop if it’s a terminator
if (IsTerminatorString(sLine))
{
break;
}
// otherwise, add it to the sentence
sSentence = String.Concat (sSentence, sLine);
// let the user know how she’s doing
Console.WriteLine(“\nYou’ve entered: (0)”, sSentence);
}
Console.WriteLine(“\nTotal sentence:\n(0)”, sSentence);
// wait for user to acknowledge the results
Console.WriteLine(“Press Enter to terminate…”);
Console.Read();
}
// IsTerminating – return a true if the source string is equal to any of the termination strings public static bool IsTerminateString(string source)
{
string[] sTerms = {“EXIT”, “exit”, “QUIT”, “quit”);
// compare the string entered to each of the legal exit commands
foreach(string sTerm in sTerms)
{
// return a true if you have a match
if (String.Compare(source, sTerm) == 0)
{
return true;
}}
return false;
}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment