Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
122 views5 pages

C# Array and String Exercises

The document contains code examples for performing various operations on arrays and strings in C#. These include examples to read and print elements of an array, count the number of alphabets, digits, and special characters in a string, count the number of vowels and consonants in a string, and find the maximum occurring character in a string.

Uploaded by

Chem S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views5 pages

C# Array and String Exercises

The document contains code examples for performing various operations on arrays and strings in C#. These include examples to read and print elements of an array, count the number of alphabets, digits, and special characters in a string, count the number of vowels and consonants in a string, and find the maximum occurring character in a string.

Uploaded by

Chem S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

W3 school Arrays

1) Read and Print elements of an array

using System;

public class Exercise1

public static void Main()

int[] arr = new int[10];

int i;

Console.Write("\n\nRead and Print elements of an array:\n");

Console.Write("-----------------------------------------\n");

Console.Write("Input 10 elements in the array :\n");

for(i=0; i<10; i++)

Console.Write("element - {0} : ",i);

arr[i] = Convert.ToInt32(Console.ReadLine());

Console.Write("\nElements in array are: ");

for(i=0; i<10; i++)

Console.Write("{0} ", arr[i]);

Console.Write("\n");

..

using System;
public class Exercise7

public static void Main()

string str;

int alp, digit, splch, i,l;

alp = digit = splch = i = 0;

Console.Write("\n\nCount total number of alphabets, digits and special characters :\n");

Console.Write("--------------------------------------------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

l=str.Length;

/* Checks each character of string*/

while(i<l)

if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))

alp++;

else if(str[i]>='0' && str[i]<='9')

digit++;

else

splch++;

}
i++;

Console.Write("Number of Alphabets in the string is : {0}\n", alp);

Console.Write("Number of Digits in the string is : {0}\n", digit);

Console.Write("Number of Special characters in the string is : {0}\n\n", splch);

,.

using System;

public class Exercise9

public static void Main()

string str;

int i, len, vowel, cons;

Console.Write("\n\nCount total number of vowel or consonant :\n");

Console.Write("----------------------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

vowel = 0;

cons = 0;

len = str.Length;

for(i=0; i<len; i++)

{
if(str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' ||
str[i]=='I' || str[i]=='O' || str[i]=='U')

vowel++;

else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))

cons++;

Console.Write("\nThe total number of vowel in the string is : {0}\n", vowel);

Console.Write("The total number of consonant in the string is : {0}\n\n", cons);

using System;

public class Exercise10

public static void Main()

string str;

int[] ch_fre = new int[255];

int i = 0, max,l;

int ascii;

Console.Write("\n\nFind maximum occurring character in a string :\n");

Console.Write("--------------------------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

l=str.Length;
for(i=0; i<255; i++) //Set frequency of all characters to 0

ch_fre[i] = 0;

/* Read for frequency of each characters */

i=0;

while(i<l)

ascii = (int)str[i];

ch_fre[ascii] += 1;

i++;

// Console.Write("{0} ",(char)65);

max = 0;

for(i=0; i<255; i++)

if(i!=32)

if(ch_fre[i] > ch_fre[max])

max = i;

Console.Write("The Highest frequency of character '{0}' is appearing for number of times : {1}
\n\n", (char)max, ch_fre[max]);

You might also like