You are on page 1of 8

1.

to reverse the given string



Console.WriteLine("Enter string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
Array.Reverse(ch);
string str1 = new string(ch);
Console.WriteLine("String after reverse is:" + str1);
Console.Read();

2. To count no of vowels and consonents in a given string

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
str = str.ToLower();
int vow = 0, cons = 0;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{

switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
break;

default:
cons++;
break;

}
}
}
Console.WriteLine("vowels:" + vow);
Console.WriteLine("Consonants:" + cons);
Console.Read();


3.to convert the first letter of a string to upper case

Console.WriteLine("Enter new string");
string str = Console.ReadLine();
string s = str.Substring(0,1).ToUpper() + str.Substring(1);
Console.WriteLine(s);
Console.Read();

4. to convert string to lower case and vice versa

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
string lower = str.ToLower();
string upper = str.ToUpper();
Console.WriteLine("Lower case:"+lower);
Console.WriteLine("Upper case:"+upper);
Console.Read();

5. To Convert starting letter of each word in a string to upper case

Console.WriteLine("Enter new string");
string str = Console.ReadLine();
string s =
Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
Console.WriteLine(s);
Console.Read();

OR

Console.WriteLine("Enter new string");
string str = Console.ReadLine();
string[] s = str.Split(' ');
string res = string.Empty;
int c = 0;
foreach (string a in s)
{
if (c == 0)
{
res = a.Substring(0, 1).ToUpper() + a.Substring(1);
c++;
}
else
{
res = res + " " + a.Substring(0, 1).ToUpper() + a.Substring(1);
}
}
Console.WriteLine(res);
Console.Read();


6. to convert alternate characters of a string to upper case

Console.WriteLine("Enter new string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
char[] ch1 = new char[100];

int n = str.Length;
int i=0;
while(i<n)
{
if (i % 2 == 0)
{
ch1[i] = char.ToUpper(ch[i]);
i++;
}
else
{
ch1[i] = char.ToLower(ch[i]);
i++;
}
}
string fin = new string(ch1);
Console.WriteLine("Result is: "+fin);
Console.Read();


7. Replacing give character with special character

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
Console.WriteLine("Enter any character");
char ch = char.Parse(Console.ReadLine());
string res = str.Replace(ch, '@');
Console.WriteLine("After replacing: "+ res);
Console.Read();

8. Replacing given index of a string with special character

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
Console.WriteLine("Enter index value");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter special character");
char sp = char.Parse(Console.ReadLine());
char[] ch = str.ToCharArray();
ch[n] = sp;
string res = new string(ch);
Console.WriteLine("Result is:"+res);
Console.Read();


9. To count no of characters in a string

Console.WriteLine("Enter any string");
string str = Console.ReadLine();
str=str.ToLower();
int count = 0;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{
count++;
}
}
Console.WriteLine("No of characters are:"+count);
Console.Read();

10. To find maximum and minimum length words in a given string

Console.WriteLine("Enter any string");
string str = Console.ReadLine();
string[] ar = str.Split(' ');
int max = 0, min = 0;
string str1 = string.Empty;
string str2 = string.Empty;
max= ar[0].Length;
min= ar[0].Length;

foreach (string st in ar)
{
int n = st.Length;
if (n >= max)
{
str1 = st;
max = n;
}
if (n <= min)
{
str2 = st;
min = n;

}
}
Console.WriteLine("max length word is:"+str1);
Console.WriteLine("Max length is:"+max);
Console.WriteLine("Min length word is:"+str2);
Console.WriteLine("Min length is:"+min);
Console.Read();



11. To sort given array in ascending or descending order

Console.WriteLine("Enter size of array");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
Console.WriteLine("Enter array elements");
for (int i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Array in ascending order is:");

Array.Sort(a);
Console.WriteLine("Array in ascending order:");
foreach (int k in a)
{
Console.WriteLine(k + ",");
}
Array.Reverse(a);
Console.WriteLine("Array in descending order :");
foreach(int l in a)
{
Console.WriteLine(l+",");
}
Console.Read();


12. Creating mirror image of a string

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
Array.Reverse(ch);
string str1 = new string(ch);
string res = str + '|' + str1;
Console.WriteLine("Result is:"+res);
Console.Read();

13. Program to replace consonents with next alphabet

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
char[] ch1 = str.ToCharArray();
char[] ch = new char[100];
int i = 0;
foreach (char c in ch1)
{

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'
|| c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
ch[i] = c;
i++;
}
else
{
int z = c;
z++;
ch[i] = (char)z;
i++;
}
}
string res = new string(ch);
Console.WriteLine("Result is: "+res);
Console.Read();




731-->(7-3)(3-1)-->42.

Console.WriteLine("Enter any number");
int n = int.Parse(Console.ReadLine());
string str = n.ToString();
char[] ch = str.ToCharArray();
Array.Reverse(ch);
int[] res = new int[10];
string str1 = new string(ch);
int n1 = int.Parse(str1);
int x=0;
int prev = n1 % 10;
n1 = n1 / 10;
while (n1 != 0)
{

int next = n1 % 10;
n1 = n1 / 10;
res[x] = prev - next;
x++;
prev = next;
}
for (int i = 0; i < x; i++)
{
Console.WriteLine(res[i]);
}
Console.Read();




14. To print count of each character in an character array ** incomplete **

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
char[] res = new char[100];
int[] res1 = new int[100];
Array.Reverse(ch);
int z = 0;
for (int i = 0; i < ch.Length; i++)
{
int count = 0;
char temp = ch[i];
for (int j = i; j < ch.Length; j++)
{
if (temp == ch[j])
{
i++;
count++;
}
}
res[z] = temp;
z++;
res1[z] = count;
z++;
count = 0;
}
for(int g=0;g<z;g++)
{
Console.WriteLine(res[g] + ",");
}
Console.Read();


15.To check weather both the strings contains same characters in different
order

Console.WriteLine("Enter string1");
string str1 = Console.ReadLine();
Console.WriteLine("Enter string2");
string str2 = Console.ReadLine();
char[] ch1 = str1.ToCharArray();
Array.Sort(ch1);
char[] ch2 = str2.ToCharArray();
Array.Sort(ch2);
string str3 = new string(ch1);
string str4 = new string(ch2);
int n = string.Compare(str3, str4);
if (n == 0)
{
Console.WriteLine("Both strings are same");
}
else
{
Console.WriteLine("Both are different strings");
}
Console.Read();


16. To replace alternate characters with '*'

Console.WriteLine("Enter new string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
char[] ch1 = new char[100];

int n = str.Length;
int i = 0;
while (i < n)
{
if (i % 2 == 0)
{
ch1[i] = ch[i];
i++;
}
else
{
ch1[i] = '*';
i++;
}
}
string fin = new string(ch1);
Console.WriteLine("Result is: " + fin);
Console.Read();


17. Remove duplicate characters in an array

Console.WriteLine("Enter a string");
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
char[] ch1 = new char[20];
Array.Sort(ch);
int k = 0;
char temp = ch[0];
for (int i = 0; i < ch.Length; i++)
{

for (int j = i; j < ch.Length; j++)
{
if (ch[j] == temp)
{
i++;
}
}
temp = ch[i];
ch1[k] = temp;
k++;

}
Console.WriteLine("Array after removing duplicates:");
for (int i = 0; i < k; i++)
{
Console.WriteLine(ch1[i] + ",");
}
Console.Read();


18. Same as program6

You might also like