- Reverse Each Word in a Sentence without using any Function/String Variables in C#
- string sentense = "This is a good day";
- char[] arr = sentense.ToCharArray();
- int temp = 0;
- //Logic to iterate up to end of the line.
- for (int fl = 0; fl <= arr.Length - 1;fl++ )
- {
- int count = temp;
- int num1 = 1;
- //To get the word before space or the last word
- if (arr[fl] == ' ' || fl == arr.Length - 1)
- {
- if (fl == arr.Length - 1)
- {
- for (int c = fl; c >= temp; c--)
- {
- //Swap the word
- if (num1 <= (fl - temp) / 2)
- {
- char tempC = arr[count];
- arr[count] = arr[c];
- arr[c] = tempC;
- count++;
- num1++;
- }
- }
- }
- else
- {
- for (int c = fl - 1; c >= temp; c--)
- {
- if (num1 <= (fl - temp) / 2)
- {
- char tempC = arr[count];
- arr[count] = arr[c];
- arr[c] = tempC;
- count++;
- num1++;
- }
- }
- }
- temp = fl + 1;
- }
- }
- string newLine = new string(arr);
2) Find Second largest digit in given number without using any collection like array list
int n= 4719;
int max=0, x=0, secondhighestnum =0;
while(n>0)
{
x=n%10;
if(x>max)
{
max =x;
}
if(x > secondhighestnum && x < max)
{
secondhighestnum = x;
}
n= n/10;
}
Console.WriteLine(secondhighestnum)
public static void Main(string[] args)
{
for (int row = 8; row >= 1; --row)
{
for (int col = 1; col <= row; ++col)
{
Console.Write("*");
}
Console.WriteLine();
}
}
Output
********
*******
******
*****
****
***
**
*
http://www.csharpstar.com/star-pattern-programs-in-csharp/
2)
public class Program
{
public static void Main(string[] args)
{
for (int row = 1; row <= 8; ++row)
{
for (int col = 1; col <= row; ++col)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output
*
**
***
****
*****
******
*******
********
3)
public class Program
{
public static void Main(string[] args)
{
int val = 8;
int i, j, k;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= val - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
Output