-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
195 lines (165 loc) · 5.61 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Text;
namespace C_Sharp_interview_questions_01
{
class Program
{
static void Main(string[] args)
{
string str = "The Quick Brown Fox";
int[] numArray = { 1, 6, 9, 7, 3, 4 };
ReverseString(str);
ReverseSentance(str);
ReverseWords(str);
CountCharactors("Hello World Good Morning");
RemoveDuplicates("HelloWorld");
AllSubstring("ABCD");
LargestNumber(numArray);
ThirdLargestNumber(numArray);
}
//01. How to Reverse string
public static void ReverseString(string str)
{
char[] charArray = str.ToCharArray();
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
{
charArray[i] = str[j];
charArray[j] = str[i];
}
string reverseString = new string(charArray);
Console.WriteLine("{0} ==> {1}", str, reverseString);
}
//02.Reverse the order of words in given string
public static void ReverseSentance(string str)
{
string[] stringArray = str.Split(' ');
string reverse = string.Empty;
for (int i = stringArray.Length - 1; i >= 0; i--)
{
if (i == stringArray.Length - 1)
{
reverse = stringArray[i];
}
else
{
reverse = reverse + ' ' + stringArray[i];
}
}
Console.WriteLine("{0} ==> {1}", str, reverse);
}
//03.reverse each word in a given string
public static void ReverseWords(string str)
{
string[] strArray = str.Split(' ');
string finalString = string.Empty;
for (int k = 0; k < strArray.Length - 1; k++)
{
char[] charArray = strArray[k].ToCharArray();
for (int i = 0, j = strArray[k].Length - 1; i < j; i++, j--)
{
charArray[i] = strArray[k][j];
charArray[j] = strArray[k][i];
}
if (k != 0)
{
finalString = finalString + ' ' + new string(charArray);
}
else
{
finalString = new string(charArray);
}
}
Console.WriteLine("{0} ==> {1}", str, finalString);
}
//04.count the occurrence of each character in a string
public static void CountCharactors(string str)
{
var charactorCount = new Dictionary<char, int>();
foreach (var charactor in str)
{
if (charactor != ' ')
{
if (!charactorCount.ContainsKey(charactor))
{
charactorCount.Add(charactor, 1);
}
else
{
charactorCount[charactor]++;
}
}
}
foreach (var charactor in charactorCount)
{
Console.WriteLine("{0} - {1}", charactor.Key, charactor.Value);
}
}
//05.Remove dupilcate charactors from string
public static void RemoveDuplicates(string str)
{
string result = string.Empty;
for (int i = 0; i < str.Length - 1; i++)
{
if (!result.Contains(str[i]))
{
result += str[i];
}
}
Console.WriteLine("{0} ==> {1}", str, result);
}
//06.All possible substring of a given string
public static void AllSubstring(string str)
{
for (int i = 0; i < str.Length; i++)
{
var subString = new StringBuilder(str.Length - 1);
for (int j = i; j < str.Length - 1; j++)
{
subString.Append(str[j]);
Console.WriteLine("{0}", subString);
}
}
}
//07.Find second largest integer in an array using only one
public static void LargestNumber(int[] numArray)
{
int tempMax = int.MinValue;
for (int i = 0; i < numArray.Length; i++)
{
if (tempMax < numArray[i])
{
tempMax = numArray[i];
}
}
Console.WriteLine("{0} , {1}", "Maximum number is ", tempMax);
}
//08. Find third largest integer in an array using only one loop?
public static void ThirdLargestNumber(int[] numArray)
{
int max1 = int.MinValue;
int max2 = int.MinValue;
int max3 = int.MinValue;
for (int i = 0; i < numArray.Length; i++)
{
if (max1 < numArray[i])
{
max1 = numArray[i];
}
if (max1 > max2)
{
int temp = max2;
max2 = max1;
max1 = temp;
}
if (max2 > max3)
{
int temp = max3;
max3 = max2;
max2 = temp;
}
}
Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}", "Third Largest Number is ", max3, "Second Largest Number is ", max2, "Largest Number is ", max1);
}
}
}