using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TheStringClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStringAsArrayOfChar_Click(object sender, EventArgs e)
{
// get the input string
string inputstr = txtInputString.Text;
if (inputstr != String.Empty)
{
// sequence thru the string
// A string is an array of characters (char)
richTextBox1.Clear();
int counter = 0;
foreach (char ch in inputstr)
{
if (ch == 'a' || ch == 'A')
counter++;
if (Char.IsDigit(ch))
richTextBox1.AppendText(ch + ": digit" + "\n");
if (char.IsLetter(ch))
richTextBox1.AppendText(ch + ": letter" + "\n");
if (char.IsNumber(ch))
richTextBox1.AppendText(ch + ": number" + "\n");
if (char.IsPunctuation(ch))
richTextBox1.AppendText(ch + ": punctuation" + "\n");
if (char.IsSeparator(ch))
richTextBox1.AppendText(ch + ": separator" + "\n");
}
richTextBox1.AppendText("\n" + "There are " + counter + " a's");
}
}
//1. Add all the digits in the input string / display sum
private void btnAddAllDigitFromString_Click(object sender, EventArgs e)
{
int sum = 0;
string inputString = txtInputString.Text;
foreach (char c in inputString)
{
if (char.IsDigit(c))
sum += int.Parse(c.ToString());
}
richTextBox1.Text = "Sum of all the digits: " + sum;
}
private void btnExtractAllDigit_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
string digitnum = String.Empty;
if (inputstr != String.Empty)
{
richTextBox1.Clear();
foreach (char ch in inputstr)
{
if (Char.IsDigit(ch))
{
digitnum += ch.ToString();
}
//if you need to convert it to an int, then parse it
richTextBox1.Text = "Extracted Digits: " + digitnum;
}
}
}
private void btnSwapFirstAndLast_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
string firstAndLastSwapped = SwapFirstAndLast(inputstr);
richTextBox1.Text = firstAndLastSwapped;
}
private string SwapFirstAndLast(string inputstring)
{
char[] chars = inputstring.ToCharArray();
if (inputstring != String.Empty)
{
char temp = chars[0];
chars[0] = chars[chars.Length - 1];
chars[chars.Length - 1] = temp;
}
return new string(chars); // RETURN THE NEW STRING
//char[] carray = inputstr.ToCharArray();
//char temp = carray[0];
//carray[0] = carray[carray.Length - 1];
//carray[carray.Length - 1] = temp;
//return new String(carray);
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInputString.Clear();
richTextBox1.Clear();
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void btnIndexOfFirstAndLast_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
int firstSpace, lastSpace;
firstSpace = inputstr.IndexOf(" ");
lastSpace = inputstr.LastIndexOf(" ");
if (firstSpace != -1 || lastSpace != -1)
{
richTextBox1.AppendText("First space: " + firstSpace.ToString() +
"\n" + "Last Space:" + lastSpace.ToString());
}
}
private void btnIndexOf1_Click(object sender, EventArgs e)
{
//get input string
string inputstr = txtInputString.Text;
//get character to search
char ch = char.Parse(txtFirstChar.Text);
//search for first occurrence
int index = inputstr.IndexOf(ch);
if (index >= 0)
richTextBox1.Text = ch + " was found at index: " + index;
else
richTextBox1.Text = ch + " could not be found";
}
private void btnIndexOfLast_Click(object sender, EventArgs e)
{
//get input string
string inputstr = txtInputString.Text;
//get sub string
string substr = txtFirstSubStr.Text;
//get first occurrence
int index = inputstr.IndexOf(substr);
if (index >= 0)
richTextBox1.Text = substr + "\nwas found at index: " + index;
else
richTextBox1.Text = substr + "\ncould not be found";
}
private void btnGetLastIndexOfChar_Click(object sender, EventArgs e)
{
//get input string
string inputstr = txtInputString.Text;
//get character to search
char ch = char.Parse(txtLastChar.Text);
//search for last occurrence
int index = inputstr.LastIndexOf(ch);
if (index >= 0)
richTextBox1.Text = ch + " was found at index: " + index;
else
richTextBox1.Text = ch + " could not be found";
}
private void btnSplit_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
char[] separators = { ' ', ',', ';', '.', '?' };
//the split method returns an array of strings
string[] words = inputstr.Split(separators);
//or
//string words = inputstr.Split(new char[] {' ', ',', ';', '.', '?'};
//or using the params
//string[] words = inputstr.Split(' ', ',', ';', '.', '?' )
//display the string array
richTextBox1.Clear();
foreach (string w in words)
richTextBox1.AppendText(w + "\n");
}
private void btnSplitInbetterway_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
char[] separators = { ' ', ',', ';', '.', '?' };
//the Split method returns an array of strings
string[] words = inputstr.Split(separators, StringSplitOptions.RemoveEmptyEntries);
richTextBox1.Clear();
foreach (string w in words)
richTextBox1.AppendText(w + "\n");
}
private void btnSubString_Click(object sender, EventArgs e)
{
try
{
string inputstr = txtInputString.Text;
int startIndex = int.Parse(txtIndex.Text);
int length = int.Parse(txtLength.Text);
//get the substring that start at startIndex
//and whose length is 'length'
string substr = inputstr.Substring(startIndex, length);
//display it
richTextBox1.Text = substr;
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
}
catch (ArgumentOutOfRangeException ae)
{
MessageBox.Show(ae.Message);
}
}
}
}
//Lab Assignment
//+++++++++++++
//Given an input string:
//1. Add all the digits in the input string / display sum
//2. Extract all the digits into a single string,
//parse it and display result
///3. Swap the first character with the last character
///display the resulting string
//4. Research the indexOf method, and LastIndexof methods,
// then determine the index of the first and
// last space in the input string
//Note: //msdn.microsoft.com/en-us/library/system.string.indexof.aspx
//Note: read more about "Char structure"
//======================LAB ASSIGNMENT FOR MONDAY 6/16/14===========
//Write code to read a text file (.txt, .cs, or .java)
//and determine the number of occurrences of each character
//in the alphabet.
//Write code to read a .cs file and determine the number of for loops and if statements it has.
//=====================LAB ASSINGMENT FOR TUESDAY 6/17===============
//Create a new project where user can enter main string
//Determine the index of the second whitespace
//Determine the number of words in the main string
//Swap the first and last words
//Insert a new word (or substring) right before the last word of the main string.
//Create an array of string.Populate it with names
//Use a foreach that displays only the names that start
//with a some character. (This character is read from user).

No comments:
Post a Comment