Compiler Computing Theory - Practical exam
cheack identifaier
bool flag = true;
Console.Write("Ener String: ");
string s = Console.ReadLine();
int x = 0;
if (s[0] >= '0' && s[0] <= '9')
{
x = s.Length;
flag = false;
}
for (int i = x; i < s.Length; i++)
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] > 'A' && s[i] < 'Z') || s[i] == '_')
{
continue;
}
else
{
flag = false;
break;
}
if (flag)
Console.WriteLine("Identifier");
else
Console.WriteLine("Not Identifier");
Console.ReadLine();
-------------------------------------------------------------------------------------------------
cheack Integer
bool flag = true;
int x = 0;
string s = Console.ReadLine();
if (s[0] == '-'||s[0]== '+')
x = 1;
for (int i = x; i < s.Length; i++)
if (s[i] >= '0' && s[i] <= '9')
{ }
else
{
flag = false;
break;
}
if (flag)
Console.WriteLine("Integer Number");
else
Console.WriteLine("Not Integer Number");
Console.ReadLine();
-------------------------------------------------------------------------------------------------
cheack Floute
string num = Console.ReadLine();
bool flag = false;
int i, x = 0, no_dot = 0,k=0;
if (num[0] == '-')
{
x = 1;
k++;
}
for (i = x; i < num.Length; i++)
{
if (num[i] >= '0' && num[i] <= '9')
{
flag = true;
k++;
}
if (num[i] == '.')
{
no_dot++;
k++;
}
}
if (num[x] == '.')
flag = false;
if (num[num.Length - 1] == '.')
flag = false;
if ((no_dot == 0) || (no_dot > 1))
flag = false;
if ((flag)&&(k==num.Length))
Console.WriteLine("Float");
else
Console.WriteLine("Not float");
Console.ReadKey();
-------------------------------------------------------------------------------------------------
Combine Tow Strings
string a,b;
int i, clen;
a = Console.ReadLine();
b = Console.ReadLine();
clen = a.Length + b.Length;
char[] c = new char[clen];
Console.Clear();
i = 0;
int j = 0;
while (i<a.Length)
{
c[j] = a[i];
i++;
j++;
}
i = 0;
while (i < b.Length)
{
c[j] = b[i];
i++;
j++;
}
Console.WriteLine(c);
Console.ReadKey();
-------------------------------------------------------------------------------------------------
Cheack keyword
string[] str = { "for", "if", "while", "break" };
string input = Console.ReadLine();
bool flag = false;
for (int i = 0; i < str.Length; i++)
if (input == str[i])
flag = true;
if (flag)
Console.WriteLine("keyword");
else
Console.WriteLine("Not");
Console.ReadKey();
-------------------------------------------------------------------------------------------------
Compare tow Strings
char[] str1 = { 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r' };
char[] str2 = { 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r' };
bool flag = true;
if (str1.Length == str2.Length)
{
for (int i = 0; i < str2.Length; i++)
if (str1[i] != str2[i])
flag = false;
}
else flag = false;
if (flag == true)
Console.WriteLine("Same");
else
Console.WriteLine("Different");
Console.ReadLine();
-------------------------------------------------------------------------------------------------
Repeat Strings
int i,j,k=0,n;
char[] str = new char[3];
Console.Write("Enter the String=");
for ( i = 0; i < str.Length; i++)
str[i] = Convert.ToChar(Console.ReadLine());
Console.Write("\n enter the value of n=");
n = int.Parse(Console.ReadLine());
Console.Clear();
char[] result = new char[str.Length * n];
for (i = 0; i < n; i++)
for (j = 0; j < str.Length; j++)
result[k++] = str[j];
Console.WriteLine(result);
Console.ReadKey();
-------------------------------------------------------------------------------------------------
Reverse String
char[] a = new char[5] { 'a', 'b', 'c', 'd', 'e' };
char[] b = new char[5];
for (int i = 0; i < a.Length; i++)
b[4 - i]=a[i];
Console.Write(b);
Console.ReadLine();
-------------------------------------------------------------------------------------------------
Subscribe to:
Posts (Atom)