namespace The_String_Class
{
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 acounter = 0;
foreach (char ch in inputstr)
{
if (ch == 'a' || ch=='A')
acounter++;
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 " + acounter + " a's");
}
}
private void btnAddDigitsOnly_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
int sum = 0;
foreach (char ch in inputstr)
{
if (Char.IsDigit(ch))
sum += int.Parse(ch.ToString());
}
richTextBox1.Text =
"Sum of all the digits: " + sum;
}
private void btnExtractDigits_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
string digitsStr = String.Empty;
foreach (char ch in inputstr)
{
if (Char.IsDigit(ch))
digitsStr += ch.ToString();
}
//if you need to convert it to an int, then parse it
richTextBox1.Text = "Extracted Digits: " + digitsStr;
}
private void button1_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 button2_Click(object sender, EventArgs e)
{
//get input string
string inputstr = txtInputString.Text;
//get character to search
char ch = char.Parse(txtFirstChar.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 btnIndexOfFisrtSubStr_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 btnSwapFirstLast_Click(object sender, EventArgs e)
{
//get input string
string inputstr = txtInputString.Text;
String swappedstr = SwapFirstLast(inputstr);
richTextBox1.Text = swappedstr;
}
private string SwapFirstLast(string inputstr)
{
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 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 btnBetterSplit_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);
//display the string array
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);
}
}
private void btnIndexOfSecondWhiteSpace_Click(object sender, EventArgs e)
{
string inputstr = txtInputString.Text;
//get index of first whitespace
char whitespace = ' ';
int index1 = inputstr.IndexOf(whitespace);
//get index of second whitespace
int index2 = inputstr.IndexOf(whitespace, index1 + 1);
richTextBox1.Text =
"Index of second whitespace is: " + index2;
}
private void btnInsertNewWord_Click(object sender, EventArgs e)
{
///Insert new word before the last word in the
///input string
string inputstr = txtInputString.Text;
if (inputstr != String.Empty)
{
//get index of first whitespace
char whitespace = ' ';
//get word to insert
string word = txtInsertWord.Text;
//=========================use the insert method in the String class=====
//get index of the last whitespace
int index = inputstr.LastIndexOf(whitespace);
if (index > 0)
{
inputstr = inputstr.Insert(index + 1, word + " ");
}
else
{
//the inputstr has one word only, insert at index:0
inputstr = inputstr.Insert(0, word + " ");
}
txtInputString.Text = inputstr;
}
}
}
}
///Lab Assignment
///++++++++++++++
///Given an iput 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 and LastIndexof methods,
/// then determine the index of the first and
/// last spaces in the input string
///====================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 ASSIGNMENT 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 STRINGS.POPULATE IT WITH NAMES
///USE A FOREACH THAT DISPLAYS ONLY THE NAMES THAT START WITH
///A SOME CHARACTER. (THIS CHARACTER IS READ FROM USER).
///
/// =====================
/// provide a way to replace any word by a new word
/// Ask user which word to replace
/// Get the new word
/// replace it
/// Display outcome

No comments:
Post a Comment