Monday, March 3, 2014

Lord of Strings



Program Generated Problems
[Nalimits.tk]
[kianaudez.blogspot.com]

If you have problems or you want me to solve anything, please post a comment :D


Getting the first three character of the product name
Textbox1.Text = the field that accepts the product name.
Method to use Substring or loop
>substring method
C#
string firstThreeLetter = TextBox1.Text.Substring(0,3);
[Good to use when product names length >= 3 else error]
String firstThreeLetter = TextBox1.Text.Substring(0,Math.Min(3,TextBox1.Text.Length());
[Good to use whatever the length of TextBox1.Text is.. Math.Min(returns 3 if the length >=3 else returns the length of the text(textbox1.text)]

LOOP METHOD
String firstThreeLetter= “”;
Foreach (char c in TextBox1.Text){
If(firstThreeLetter.Length <= 2){
                firstThreeLetter.Length += c;
}
}
[Good to use when there are conditions for example : Checking if the character is symbol or not ]
Getting the First three alpha Numeric character of the product name
                >Method To use: Loop[detects if char is letter or digit]

LOOP METHOD
String firstThreeLetter= “”;
Foreach (char c in TextBox1.Text){
If(firstThreeLetter.Length <= 2){
                If(char.IsLetterOrDigit(TextBox1.Text))
{
             
firstThreeLetter.Length += c;
}

}
}
Getting the First three Symbols of the product name
                >Method To use: Loop[detects if char is letter or digit]

LOOP METHOD
String firstThreeLetter= “”;
Foreach (char c in TextBox1.Text){
If(firstThreeLetter.Length <= 2){
                If(char.IsSymbol(TextBox1.Text))
{
             
firstThreeLetter.Length += c;
}

}
}
Checking if the first Letter is Symbol (input  assumed > 0)
                If(char.IsSymBol(TextBox1.Text[0])){ MessageBox.Show(“TRUE”); }

Checking if the first Letter is NOT Symbol (input  assumed > 0) [only accept alpha numeric]
                If(!char.IsSymBol(TextBox1.Text[0])){ MessageBox.Show(“TRUE”); }
Or
                If(!char.IsLetter(TextBox1.Text[0])){ MessageBox.Show(“TRUE”); }
Replace certain character with symbol[example: Replace vowel w/ *]
                [brute force method] disadvantage: when there are many characters to be replaced.
                TextBox1.Text = TextBox1.Text.Replace('a', '*');
TextBox1.Text = TextBox1.Text.Replace('e', '*');
TextBox1.Text = TextBox1.Text.Replace('i', '*');
TextBox1.Text = TextBox1.Text.Replace('o', '*');
TextBox1.Text = TextBox1.Text.Replace('u', '*');
[loop method]
       TextBox1.Text ="All The vowels here will be replaced. ";
      TextBox1.Text = TextBox1.Text.ToLower();

       string output = "";
       for (int x = 0; x < TextBox1.Text.Length; x++ )
            {
                if (TextBox1.Text[x] == 'a'  ||
TextBox1.Text[x] == 'e' ||
TextBox1.Text[x] == 'i' ||
TextBox1.Text[x] == 'o' ||
TextBox1.Text[x] == 'u')
                {
                    output += '*';
                }
                else
                {
                    output += TextBox1.Text[x];
                }
              
            }
Getting The First Three consonant letters
                String firstThreeLetter= “”;
Foreach (char c in TextBox1.Text){
If(firstThreeLetter.Length <= 2)
{
                                if (TextBox1.Text[x] != 'a'  ||
TextBox1.Text[x] != 'e' ||
TextBox1.Text[x] != 'i' ||
TextBox1.Text[x] != 'o' ||
TextBox1.Text[x] != 'u')
                {

firstThreeLetter.Length += c;
  }

}
}








Getting The First Three Vowel letters
                String firstThreeLetter= “”;
Foreach (char c in TextBox1.Text){
If(firstThreeLetter.Length <= 2)
{
                                if (TextBox1.Text[x] == 'a'  ||
TextBox1.Text[x] == 'e' ||
TextBox1.Text[x] == 'i' ||
TextBox1.Text[x] == 'o' ||
TextBox1.Text[x] == 'u')
                {

firstThreeLetter.Length += c;
  }

}
}
Generating a 5-digit number [4 random padded by zero]
                          Random rand = new Random();
            string randCode = "0";
            randCode += rand.Next(0, 9);
            randCode += rand.Next(0, 9);
            randCode += rand.Next(0, 9);
            randCode += rand.Next(0, 9);
            Console.WriteLine(randCode);

Removing the whitespace of string
  string TextBox1Text ="All The spaces here will be removed. ";
       string output = "";
       for (int x = 0; x < TextBox1Text.Length; x++ )
            {
                if(!char.IsWhiteSpace(TextBox1Text[x]))
                {
                     output += TextBox1Text[x];
                }
            }
       Console.Write(output);           
        }

      
Getting the remainder of the product quantity divided by 10
            int qty = 156;
            int remainder = qty % 10;
Getting the percentage of a value (example : 22% of 100)
            double output = 1000 * 0.22;
Getting The Current Date
       string output = DateTime.Now.ToShortDateString();

Getting The Current Day of Week (Monday,Tuesday,Wednesday etc..)
       string output = DateTime.Now.DayOfWeek.ToString();



More UPDATES soon!


      


               
               










No comments: