Read a line of string and check its length

image_pdfimage_print

using System;

class MainEntryPoint {
static void Main(string[] args) {
Console.WriteLine(“Type in a string”);
string input;
input = Console.ReadLine();
if (input == “”) {
Console.WriteLine(“You typed in an empty string”);
} else if (input.Length < 5) { Console.WriteLine("The string had less than 5 characters"); } else if (input.Length < 10) { Console.WriteLine("The string had at least 5 but less than 10 characters"); } Console.WriteLine("The string was " + input); } } [/csharp]

Echo some stats

image_pdfimage_print
   
 

using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        Console.Write("Enter your name: ");
        string s;
        s = Console.ReadLine();
        Console.WriteLine("Hello, {0} ", s);
        Console.Write("Enter your age: ");
        s = Console.ReadLine();
        Console.WriteLine("You are {0} years old", s);

    }
}

    


input a series of numbers separated by commas, parse them into integers and output the sum

image_pdfimage_print

using System;

class Class1 {
public static void Main(string[] args) {
Console.WriteLine(“Input a series of numbers separated by commas:”);
string input = Console.ReadLine();
char[] cDividers = { ',', ' ' };
string[] segments = input.Split(cDividers);
int nSum = 0;
foreach (string s in segments) {
if (s.Length > 0) {
if (IsAllDigits(s)) {
int num = Int32.Parse(s);
Console.WriteLine(“Next number = {0}”, num);
nSum += num;
}
}
}
Console.WriteLine(“Sum = {0}”, nSum);

}

public static bool IsAllDigits(string sRaw) {
string s = sRaw.Trim();
if (s.Length == 0) {
return false;
}

for (int index = 0; index < s.Length; index++) { if (Char.IsDigit(s[index]) == false) { return false; } } return true; } } [/csharp]

constructs sentences by concatenating user input until the user enters one of the termination characters

image_pdfimage_print
   
 

using System;

public class MainClass {
    public static void Main(string[] args) {
        string sSentence = "";
        for (; ; ) {
            Console.WriteLine("Enter a string");
            string sLine = Console.ReadLine();
            if (IsTerminateString(sLine)) {
                break;
            }
            sSentence = String.Concat(sSentence, sLine);
            Console.WriteLine("
You&#039;ve entered: {0}", sSentence);
        }
    }
    public static bool IsTerminateString(string source) {
        string[] sTerms = {"EXIT","exit","QUIT","quit"};
        foreach (string sTerm in sTerms) {
            if (String.Compare(source, sTerm) == 0) {
                return true;
            }
        }
        return false;
    }
}

    


Convert input from control to upper case

image_pdfimage_print
   
 
using System;

public class MainClass {
    static void Main() {
        Console.WriteLine("Enter command:");
        string resp = (Console.ReadLine()).ToLower();
        switch (resp) {
            case "a":
                Console.WriteLine("Doing Task A");
                break;
            case "b":
                Console.WriteLine("Doing Task B");
                break;
            case "c":
                Console.WriteLine("Doing Task C");
                break;
            default:
                Console.WriteLine("Bad choice");
                break;
        }
    }
}