Q 1) Write a program in C# Sharp to reverse a string?
Answer:
internal 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 reversedstring = new string(charArray);
Console.WriteLine(reversedstring);
}
Q 2) Write a program in C# Sharp to reverse the order of the given words?
Answer:
internal static void ReverseWordOrder(string str)
{
int i;
StringBuilder reverseSentence = new StringBuilder();
int Start = str.Length - 1;
int End = str.Length - 1;
while (Start > 0)
{
if (str[Start] == ' ')
{
i = Start + 1;
while (i <= End)
{
reverseSentence.Append(str[i]);
i++;
}
reverseSentence.Append(' ');
End = Start - 1;
}
Start--;
}
for (i = 0; i <= End; i++)
{
reverseSentence.Append(str[i]);
}
Console.WriteLine(reverseSentence.ToString());
}
Q 3) Write a program in C# Sharp to find if a given string is palindrome or not?
Answer:
internal static void chkPalindrome(string str)
{
bool flag = false;
for (int i = 0, j = str.Length - 1; i < str.Length / 2; i++, j--)
{
if (str[i] != str[j])
{
flag = false;
break;
}
else
flag = true;
}
if (flag)
{
Console.WriteLine("Palindrome");
}
else
Console.WriteLine("Not Palindrome");
}
Q 4) Write a C# program to find the substring from a given string.
Answer:
internal static void findallsubstring(string str)
{
for (int i = 0; i < str.Length; ++i)
{
StringBuilder subString = new StringBuilder(str.Length - i);
for (int j = i; j < str.Length; ++j)
{
subString.Append(str[j]);
Console.Write(subString + " ");
}
}
}
Q 5) Write a C# program to find if a positive integer is prime or not?
Answer:
static void Main(string[] args)
{
if (FindPrime(47))
{
Console.WriteLine("Prime");
}
else
{
Console.WriteLine("Not Prime");
}
Console.ReadLine();
}
No comments:
Post a Comment