0% found this document useful (0 votes)
58 views4 pages

C# String Analysis Programs

The document contains multiple C# Sharp programs that demonstrate various string manipulation techniques. These include finding the position of a specified word, counting total words and characters, counting alphabets, digits, and special characters, and counting vowels and consonants in a string. Each program is structured with a main method and relevant logic to perform the specified tasks.

Uploaded by

Anupam Silwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

C# String Analysis Programs

The document contains multiple C# Sharp programs that demonstrate various string manipulation techniques. These include finding the position of a specified word, counting total words and characters, counting alphabets, digits, and special characters, and counting vowels and consonants in a string. Each program is structured with a main method and relevant logic to perform the specified tasks.

Uploaded by

Anupam Silwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.";

Console.WriteLine("Original string: " + str1);


Console.WriteLine("Position of the word 'fox': " + f.GetResult(str1,
"fox"));
Console.WriteLine("Position of the word 'The': " + f.GetResult(str1,
"The"));
Console.WriteLine("Position of the word 'lazy': " + f.GetResult(str1,
"lazy"));
Console.ReadLine();
}
}
}
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 = text.Split(' ');
int a = Array.IndexOf(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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

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 = inputString.Length;

Console.WriteLine("Total number of words: " + wordCount);


Console.WriteLine("Total number of characters: " + charCount);
Console.ReadLine();
}
static int CountWords(string input)
{
string[] words = input.Split(' ');
return words.Length;

}
}
}

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 (Char.IsLetter(character))
{
alphabetCount++;
}
else if (Char.IsDigit(character))
{
digitCount++;
}
else
{
specialCharCount++;
}
}

Console.WriteLine("Number of Alphabets: " + alphabetCount);


Console.WriteLine("Number of Digits: " + digitCount);
Console.WriteLine("Number of Special Characters: " +
specialCharCount);
Console.ReadLine();
}
}
}

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 = inputString.ToLower();
int vowelCount = 0;
int consonantCount = 0;
foreach (char character in inputString)
{
// Check if the character is an alphabet letter
if (Char.IsLetter(character))
{
// Check if the letter is a vowel
if ("aeiou".Contains(character))
{
vowelCount++;
}
else
{
consonantCount++;
}
}
}

Console.WriteLine("Number of vowels: " + vowelCount);


Console.WriteLine("Number of consonants: " + consonantCount);
}
}
}

You might also like