1.
Write a C# Sharp program to find the position of a specified word in a
given string.
Sample Example:
Text: The quick brown fox jumps over the lazy dog.
Position: 1 2 3 4 5 6 7 8 9
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Finder f = new Finder();
string str1 = "The quick brown fox jumps over the lazy dog.";
[Link]("Original string: " + str1);
[Link]("Position of the word 'fox': " + [Link](str1,
"fox"));
[Link]("Position of the word 'The': " + [Link](str1,
"The"));
[Link]("Position of the word 'lazy': " + [Link](str1,
"lazy"));
[Link]();
}
}
}
public class Finder
{
public int GetResult(string text, string word)
{
// Split the input text into words and find the index (position) of the
word
// Adding +1 to make (starting from 1 instead of 0)
string[] str = [Link](' ');
int a = [Link](str, word)+1;
return a;
}
}
2. Write a program in C# Sharp to count the total number of words and Character in a
string.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp58
{
internal class Program
{
static void Main(string[] args)
{
string inputString = "This is a sample string for counting words
and characters.";
// Counting words
int wordCount = CountWords(inputString);
// Counting characters (including spaces)
int charCount = [Link];
[Link]("Total number of words: " + wordCount);
[Link]("Total number of characters: " + charCount);
[Link]();
}
static int CountWords(string input)
{
string[] words = [Link](' ');
return [Link];
}
}
}
Write a program in C# Sharp to count the number of alphabets, digits and
special characters in a string
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
string inputString = "Hi Sunil1! Welcome to Kathford?";
int alphabetCount = 0;
int digitCount = 0;
int specialCharCount = 0;
foreach (char character in inputString)
{
if ([Link](character))
{
alphabetCount++;
}
else if ([Link](character))
{
digitCount++;
}
else
{
specialCharCount++;
}
}
[Link]("Number of Alphabets: " + alphabetCount);
[Link]("Number of Digits: " + digitCount);
[Link]("Number of Special Characters: " +
specialCharCount);
[Link]();
}
}
}
Write a C# Sharp program to count the number of vowels or consonants in a
string.
namespace ConsoleApp5
{
internal class Program
{
static void Main(string[] args)
{
string inputString = "Hello World";
inputString = [Link]();
int vowelCount = 0;
int consonantCount = 0;
foreach (char character in inputString)
{
// Check if the character is an alphabet letter
if ([Link](character))
{
// Check if the letter is a vowel
if ("aeiou".Contains(character))
{
vowelCount++;
}
else
{
consonantCount++;
}
}
}
[Link]("Number of vowels: " + vowelCount);
[Link]("Number of consonants: " + consonantCount);
}
}
}