-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (119 loc) · 5.32 KB
/
Copy pathProgram.cs
File metadata and controls
136 lines (119 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
namespace StringManipulation
{
class Program
{
// 1. Character Counter Method
public static void AnalyzeString(string text)
{
int uppercaseCount = 0; // To store the count of uppercase letters
int lowercaseCount = 0; // To store the count of lowercase letters
int digitCount = 0; // To store the count of digits
int spaceCount = 0; // To store the count of spaces
int otherCount = 0; // To store the count of other characters
// Loop through each character in the input string
for (int i = 0; i < text.Length; i++)
{
char currentChar = text[i]; // Get the current character
if (char.IsUpper(currentChar)) // Check if the character is uppercase
{
uppercaseCount++;
}
else if (char.IsLower(currentChar)) // Check if the character is lowercase
{
lowercaseCount++;
}
else if (char.IsDigit(currentChar)) // Check if the character is a digit
{
digitCount++;
}
else if (char.IsWhiteSpace(currentChar)) // Check if the character is a whitespace (like space, tab, newline)
{
spaceCount++;
}
else // If none of the above, it's considered another special character
{
otherCount++;
}
}
// Print the counts to the console
Console.WriteLine($"Original String: {text}");
Console.WriteLine($"Uppercase Letters: {uppercaseCount}");
Console.WriteLine($"Lowercase Letters: {lowercaseCount}");
Console.WriteLine($"Digits: {digitCount}");
Console.WriteLine($"Spaces: {spaceCount}");
Console.WriteLine($"Other Special Characters: {otherCount}");
Console.WriteLine(); // Add an empty line for better readability
}
// 2. Palindrome Checker Method
public static bool IsPalindrome(string text)
{
// Convert the string to lowercase to ignore case
string lowerCaseText = text.ToLower();
// Remove all spaces from the string
string noSpaceText = "";
foreach (char c in lowerCaseText)
{
if (!char.IsWhiteSpace(c))
{
noSpaceText += c;
}
}
// Use two pointers, one at the start and one at the end
int left = 0;
int right = noSpaceText.Length - 1;
// Loop as long as the left pointer is before the right pointer
while (left < right)
{
// If the characters at the left and right are not the same, it's not a palindrome
if (noSpaceText[left] != noSpaceText[right])
{
return false; // Return false immediately
}
left++; // Move the left pointer one step to the right
right--; // Move the right pointer one step to the left
}
// If the loop completes without finding any mismatched characters, it's a palindrome
return true;
}
// 3. Word Reverser Method
public static string ReverseWords(string sentence)
{
// Split the sentence into an array of words using space as the delimiter
string[] words = sentence.Split(' ');
// Reverse the order of the words in the array
Array.Reverse(words);
// Join the reversed words back into a string with spaces in between
string reversedSentence = string.Join(" ", words);
return reversedSentence;
}
static void Main(string[] args)
{
Console.WriteLine("-------String Manipulation App!-------");
Console.WriteLine("-----Yasir Maken QTS Doha Intern!-----\n");
// Demonstrate the AnalyzeString method
Console.WriteLine("--- Character Counter ---");
AnalyzeString("Hello World 123!@#");
Console.WriteLine();
// Demonstrate the IsPalindrome method
Console.WriteLine("--- Palindrome Checker ---");
Console.Write("Enter a string to check for palindrome: ");
string palindromeInput = Console.ReadLine();
if (IsPalindrome(palindromeInput))
{
Console.WriteLine($"\"{palindromeInput}\" is a palindrome.");
}
else
{
Console.WriteLine($"\"{palindromeInput}\" is not a palindrome.");
}
Console.WriteLine();
// Demonstrate the ReverseWords method
Console.WriteLine("--- Word Reverser ---");
Console.Write("Enter a sentence to reverse the words: ");
string sentenceInput = Console.ReadLine();
string reversedWords = ReverseWords(sentenceInput);
Console.WriteLine($"Reversed sentence: {reversedWords}");
}
}
}