How do you print count of duplicate characters from a string?
Solution:
You can do this by many ways, it tried to
explain the 2 ways in one ways using loop you either for loop and foreach loop
or by using IndexOf(ch) function
indexOf() : This method returns the index within this
string of the first occurrence of
the specified character or -1, if the character does not occur.
string NewString="";
foreach (Char ch in Str)
{
if (NewString.IndexOf(ch) == -1)
{
No++;
}
NewString += ch;
}
How do you check if two strings are anagrams of each other?
Solution:
According
to wiki “An anagram is word or phrase formed by rearranging the letters of a
different word or phrase, typically using all the original letters exactly
once.”
You
can do this by two way
1.By using
equal method
2. By compare
all char. Of string
You must need to sort the both array /string
then you can do by above mentioned ways
1st First way
// converting string to char array
char[] Str1Arr= str1.ToCharArray();
char[] Str2Arr = S2.ToCharArray();
Array.Sort(Str1Arr);
Array.Sort(Str2Arr);
// sort the array
for (int i = 0; i <= str1.Length - 1; i++)
{
if (Str1Arr[i] != Str2Arr[i])
{
return false;
}
}
return true;
2nd way
At place of for loop you can use equal method
if(Str1Arr.equals(Str2Arr))
{
System.out.println("Given strings are
anagrams");
} else {
System.out.println("Given strings are
not anagrams");
}
How are duplicate characters found in a string?
Solution:
How do you print count of duplicate characters from a string?
Both problem is almost same I am just giving you a hint you need
to create a else part in the code where you can store the char that are
duplicate
I am leaving on you, if you find any difficulty the please write
email, I will give you a solution
How do you count a number of vowels and consonants in a given
string?
Solution:
It’s very simple we
know that a, e, i, o, u are five
vowel rest of all are constant
int leng =
arr.Length;
foreach (char ch in arr)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
count++;
}
}
Console.WriteLine("Total Constant
" +
(leng - count));
5. How do you count the occurrence of a given character in a
string?
Solution:
In the above question we have learnt
indexOf() : This method returns the index within this
string of the first occurrence of
the specified character or -1, if the character does not occur.
public static int AccuranceofChar(string x, char find)
{
int
i = x.IndexOf(find);
return i;
}
static void Main(string[] args)
{
Program objprog = new Program();
Console.WriteLine("Char Position : "+ Program.AccuranceofChar("ShivKumar", 'v'));
Console.ReadLine();
}
6. How do you reverse words in a given sentence without using any
library method?
Solution:
public static string RevesrseAWord(string str)
{
string newString = "";
for
(int i = str.Length - 1; i >=
0; i--)
{
newString += str[i];
}
return newString;
}
static void Main(string[] args)
{
Console.WriteLine("=============================================================================");
Console.WriteLine("How do you reverse words in a given sentence without
using any library method?");
Console.WriteLine("=============================================================================");
Program objprog = new Program();
Console.WriteLine("New String :
"+ Program.RevesrseAWord("ShivKumar"));
Console.ReadLine();
}
7. How can a given string be reversed using recursion?
Solution:
public static void RevesrseAWord(string str)
{
if
((str == null) || (str.Length <= 1))
Console.Write(str);
else
{
Console.Write(str[str.Length - 1]);
RevesrseAWord(str.Substring(0,
(str.Length - 1)));
}
// return newString;
}
static void Main(string[] args)
{
Console.WriteLine("=============================================================================");
Console.WriteLine("8. How can a
given string be reversed using recursion?");
Console.WriteLine("=============================================================================");
Program objprog = new Program();
Program.RevesrseAWord("ShivKumar");
Console.ReadLine();
}
OutPut:
============================================================================
How do you reverse
words in a given sentence without using any library method?
=============================================================================
ramuKvihS
How do you check if a given string is
a palindrome?
Example
Input : madam
Output : palindrome
Input : madam
Output : Not a palindrom
Solution:
Step’s
·
First do some
validation
·
Store the string in a
char array
·
Using loop store
reverse string in another string
·
Compare the both
string
public static void Palindrom(string str)
{
if
((str == null) || (str.Length <= 1))
Console.Write(str);
else
{
str = str.ToUpper();
char[] chr = str.ToCharArray();
string str2 = "";
for (int i = chr.Length - 1; i >=
0; i--)
{
str2 += chr[i];
}
if (str.Equals(str2))
{
Console.Write("String " + str + " is palindrom ");
}
else
{
Console.Write("String " + str + " is Not a palindrom
");
}
}
}
static void Main(string[] args)
{
Console.WriteLine("=============================================================================");
Console.WriteLine("How do you check if a given string is a
palindrome?");
Console.WriteLine("=============================================================================");
Program objprog = new Program();
Program.Palindrom("madam");
Console.ReadLine();
}
No comments:
Post a Comment