C# String data type : Exercises, Practice, Solution
Write a C# Sharp program to find the length of a string without using a library function.
using System;
public class Exercise2
{
public static void Main()
{
string str; /* Declares a string of size 100 */
int l= 0;
Console.Write("Input the string : ");
str = Console.ReadLine();
foreach(char chr in str)
{
l += 1;
}
Console.Write("Length of the string is : {0}\n\n", l);
}
}
Write a program in C# Sharp to count the total number of words in a string.
using System;
public class Exercise5
{
public static void Main()
{
string str;
int i, wrd,l;
Console.Write("Input the string : ");
str = Console.ReadLine();
l = 0;
wrd = 1;
/* loop till end of string */
while (l <= str.Length - 1)
{
/* check whether the current character is white space or new line or tab character*/
if(str[l]==' ' || str[l]=='\n' || str[l]=='\t')
{
wrd++;
}
l++;
}
Console.Write("Total number of words in the string is : {0}\n", wrd);
}
}
Write a program in C# Sharp to compare two strings without using a string library functions.
using System;
public class Exercise6
{
public static void Main()
{
string str1, str2;
int flg=0;
int i = 0, l1, l2, yn = 0;
Console.Write("Input the 1st string : ");
str1 = Console.ReadLine();
Console.Write("Input the 2nd string : ");
str2 = Console.ReadLine();
l1=str1.Length;
l2=str2.Length;
/*compare checking when they are equal in length*/
if(l1==l2)
{
for(i=0;i<l1;i++)
{
if(str1[i] != str2[i])
{
yn= 1;
i= l1;
}
}
}
/*initialize the flag where they are equal, smaller and greater in length*/
if(l1 == l2)
flg=0;
else if(l1 > l2)
flg=1;
else if(l1 < l2)
flg=-1;
/*display the message where the strings are same or smaller or greater*/
if(flg == 0)
{
if(yn==0)
Console.Write("\nThe length of both strings are equal and \nalso, both strings are same.\n\n");
else
Console.Write("\nThe length of both strings are equal \nbut they are not same.\n\n");
}
else if(flg == -1)
{
Console.Write("\nThe length of the first string is smaller than second.\n\n");
}
else
{
Console.Write("\nThe length of the first string is greater than second.\n\n");
}
}
}
Write a program in C# Sharp to count the number of alphabets, digits and special characters in a string.
using System;
public class Exercise7
{
public static void Main()
{
string str;
int alp, digit, splch, i,l;
alp = digit = splch = i = 0;
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);
}
}
Write a program in C# Sharp to copy one string to another string.
using System;
public class Exercise8
{
public static void Main()
{
string str1;
int i,l;
Console.Write("\n\nCopy one string into another string :\n");
Console.Write("-----------------------------------------\n");
Console.Write("Input the string : ");
str1 = Console.ReadLine();
l=str1.Length;
string[] str2=new string[l];
/* Copies string1 to string2 character by character */
i=0;
while(i<l)
{
string tmp=str1[i].ToString();
str2[i] = tmp;
i++;
}
Console.Write("\nThe First string is : {0}\n", str1);
Console.Write("The Second string is : {0}\n", string.Join("",str2));
Console.Write("Number of characters copied : {0}\n\n", i);
}
}
Write a C# Sharp program to count the number of vowels or consonants in a string.
public class Exercise9
{
public static void Main()
{
string str;
int i, len, vowel, cons;
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);
}
}
Write a C# Sharp program to find the maximum number of characters in a string. Click here
Find the character and Number of Occurrence in given word where number of Occurrence is more then 1
string val = "Interview";
Dictionary<string, int> dict = new Dictionary<string, int>();
for (int i = 0; i < val.Length; i++)
{
string c = val[i].ToString().ToLower();
if (dict.ContainsKey(c))
{
dict[c] = dict[c] + 1;
}
else
{
dict[c] = 1;
}
}
foreach (var d in dict.Where(x=> x.Value > 1))
{
Console.WriteLine("Key: "+d.Key+" Value: "+d.Value);
}
Output:
Key: i Value: 2
Key: e Value: 2
This comment has been removed by the author.
ReplyDelete