public void StringRotate()
{
string str = "myblogpost"; //Declare a string
string checkstr = "tsopgolbym"; //String to be checked
char[] charArr = new char[str.Length]; //Intialize a new char array
int j = 0;
if (str.Length == checkstr.Length)// if both strings are equal length
{
for (int i = str.Length-1; i >=0; i--) //then continue to check rotation
{
charArr[j] = str[i];
j++;
}
string str1 = new string(charArr); // Intialize a new string
if (str1 == checkstr)
{
Console.WriteLine("Both strings are rotation strings");
}
else
{
Console.WriteLine("Both strings are not a rotation strings");
}
}
else // if both strings are not equal length
{
Console.WriteLine("Both strings are not equal length and not rotation strings");
}
}
Output: Both strings are rotation strings
Thursday, March 28, 2013
C#: Check whether an element is present in an array and find its index?
public void ElementSearch()
{
int[] eleArray = new int[5]{4,1,8,5,9}; // Assign values to array
int valSearch = 5; //Search for a value
int counter = 0; // Set a counter, if the value doesn''t exist in an array
for(int i=0; i<=4; i++) //Iterate to all elements in an array
{
if (eleArray[i] == valSearch)// Check each value is equal to value to search
{
Console.WriteLine("Value found and is in index " + i + " of an array");
}
else //if value not found, increment the counter
{
counter++;
if (counter == eleArray.Length) //counter reach arraylength, value notfound.
{
Console.WriteLine("Sorry! Value not found");
}
}
}
}
Output: Value found and is in index 3 of an array
{
int[] eleArray = new int[5]{4,1,8,5,9}; // Assign values to array
int valSearch = 5; //Search for a value
int counter = 0; // Set a counter, if the value doesn''t exist in an array
for(int i=0; i<=4; i++) //Iterate to all elements in an array
{
if (eleArray[i] == valSearch)// Check each value is equal to value to search
{
Console.WriteLine("Value found and is in index " + i + " of an array");
}
else //if value not found, increment the counter
{
counter++;
if (counter == eleArray.Length) //counter reach arraylength, value notfound.
{
Console.WriteLine("Sorry! Value not found");
}
}
}
}
Output: Value found and is in index 3 of an array
Labels:
C#
Sunday, March 17, 2013
C#: Using HashTable
public void HashTable()
{
Hashtable hash = new Hashtable();
hash.Add("Name1", "Alex");
hash.Add("Name2", "Peter");
//Gives the value of key Name2
Console.WriteLine(hash["Name2"].ToString());
//Check Name1 is on list
Console.WriteLine(hash.Contains("Name1").ToString());
//Remove Name2 key/value and check if it still contains.
hash.Remove("Name2");
Console.WriteLine(hash.Contains("Name2").ToString());
try
{
//To display an item already removed from list and catched by exception.
Console.WriteLine(hash["Name2"].ToString());
}
catch (NullReferenceException)
{
Console.WriteLine("The key value missing");
}
//Add two more value to list
hash.Add("Name2", "Sam");
hash.Add("Name3", "Kite");
//To iterates the items in hashtable.
IDictionaryEnumerator enumr=hash.GetEnumerator();
while (enumr.MoveNext())
{
Console.WriteLine(enumr.Key.ToString());
}
}
Output:Peter
True
False
The key value missing
Name1
Name2
Name3
{
Hashtable hash = new Hashtable();
hash.Add("Name1", "Alex");
hash.Add("Name2", "Peter");
//Gives the value of key Name2
Console.WriteLine(hash["Name2"].ToString());
//Check Name1 is on list
Console.WriteLine(hash.Contains("Name1").ToString());
//Remove Name2 key/value and check if it still contains.
hash.Remove("Name2");
Console.WriteLine(hash.Contains("Name2").ToString());
try
{
//To display an item already removed from list and catched by exception.
Console.WriteLine(hash["Name2"].ToString());
}
catch (NullReferenceException)
{
Console.WriteLine("The key value missing");
}
//Add two more value to list
hash.Add("Name2", "Sam");
hash.Add("Name3", "Kite");
//To iterates the items in hashtable.
IDictionaryEnumerator enumr=hash.GetEnumerator();
while (enumr.MoveNext())
{
Console.WriteLine(enumr.Key.ToString());
}
}
Output:Peter
True
False
The key value missing
Name1
Name2
Name3
Labels:
C#
C#: Replace a String
public void ReplaceString()
{
string str = "Hello how are you!";
string result= str.Replace(" ","--");
Console.WriteLine(result);
}
Output: Hello--how--are--you!
{
string str = "Hello how are you!";
string result= str.Replace(" ","--");
Console.WriteLine(result);
}
Output: Hello--how--are--you!
Labels:
C#
C#: Display Random String
public void RandomString()
{
string[] randStr = new string[5]{"hi","hello","how","are","you"};
int indexStr = rnd.Next(randStr.Length);
Console.WriteLine(randStr[indexStr]);
}
Output: hello
{
string[] randStr = new string[5]{"hi","hello","how","are","you"};
int indexStr = rnd.Next(randStr.Length);
Console.WriteLine(randStr[indexStr]);
}
Output: hello
Labels:
C#
C#: Display Random Numbers
public void RandomNumber()
{
Random rnd = new Random();
int[] randArray = new int[10];
int rand1 ;
for (int i = 0; i < 10; i++)
{
rand1 = rnd.Next(1, 10); // Random numbers between 1 and 10
Console.WriteLine(rand1);
}
}
Output: 8
2
6
4
5
7
6
3
3
2
{
Random rnd = new Random();
int[] randArray = new int[10];
int rand1 ;
for (int i = 0; i < 10; i++)
{
rand1 = rnd.Next(1, 10); // Random numbers between 1 and 10
Console.WriteLine(rand1);
}
}
Output: 8
2
6
4
5
7
6
3
3
2
Labels:
C#
Saturday, March 16, 2013
C#: Check whether a string is substring of another string?
public void CheckForSubstring()
{
string str = "onetwothree";
bool result = str.Contains("two");
Console.WriteLine(result); //returns True
result = str.Contains("Two"); //returns False, bcoz C# is case sensitive
Console.WriteLine(result);
}
Output: True
False
Labels:
C#
C#: String Reversal
public void StringReverse()
{
string str = "stringtoberevered";
char[] chararr = new char[str.Length];
chararr = str.ToCharArray();
Console.Write("The Reversed String = ");
for (int i = str.Length - 1; i >= 0; i--)
{
Console.Write(chararr[i]);
}
Console.WriteLine();
}
Output: dereverebotgnirts
{
string str = "stringtoberevered";
char[] chararr = new char[str.Length];
chararr = str.ToCharArray();
Console.Write("The Reversed String = ");
for (int i = str.Length - 1; i >= 0; i--)
{
Console.Write(chararr[i]);
}
Console.WriteLine();
}
Output: dereverebotgnirts
Labels:
C#
C#: How to find minimum value from an array of integers?
public void MinimumValInt()
{
int[] intArray = new int[7] { 10,4,5,1,7,15,12};
int minVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] < minVal)
{
minVal = intArray[i];
}
}
Console.WriteLine("Minimum value= "+minVal);
}
Output: Minimum value=1
{
int[] intArray = new int[7] { 10,4,5,1,7,15,12};
int minVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] < minVal)
{
minVal = intArray[i];
}
}
Console.WriteLine("Minimum value= "+minVal);
}
Output: Minimum value=1
Labels:
C#
Subscribe to:
Posts (Atom)