Vous êtes sur la page 1sur 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]);

Vous aimerez peut-être aussi