0% found this document useful (0 votes)
3 views22 pages

1.CSharp String Reference Guide

The C# String Practice Reference Guide is a structured study document that organizes original C# notes on string manipulation. It includes string basics, built-in methods, 28 practice problems, and additional source snippets with breakdowns and practical examples. The guide covers various topics from reversing strings to finding the longest unique substring.

Uploaded by

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

1.CSharp String Reference Guide

The C# String Practice Reference Guide is a structured study document that organizes original C# notes on string manipulation. It includes string basics, built-in methods, 28 practice problems, and additional source snippets with breakdowns and practical examples. The guide covers various topics from reversing strings to finding the longest unique substring.

Uploaded by

machinegoat120
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C# String Practice Reference Guide

Prepared from the uploaded solution-reference file

This PDF reorganizes the original commented C# notes into a structured study document. Every topic
from the source is retained, and each section includes the code, a breakdown, and a small practical
example or improvement note.

What is included
• String basics and common built-in methods
• 28 numbered practice problems from reverse-string to longest unique substring
• Extra source snippets on sorting characters and string permutations
• Added breakdowns, observations, and practical example cases

C# String Practice Reference Page 1


Index

What is included . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Index 2

String basics and built-in methods 7

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1. Reverse a given string 7

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2. Check whether a string is a palindrome 8

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3. Count the number of vowels in a string 8

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4. Count the number of consonants in a string 8

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5. Count the number of characters in the string 9

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

C# String Practice Reference Page 2


6. Count the number of words in the string 9

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

7. Convert a string to lowercase without using built-in method 10

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

8. Convert a string to uppercase without using built-in method 10

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

9. Remove all spaces between the string 11

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

10. Replace all occurrences of a character with another character 11

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

11. Remove duplicate characters from a string 11

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

12. Find the first non-repeated character in a string (nested-loop version) 12

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

C# String Practice Reference Page 3


12. Find the first non-repeated character in a string (built-in version) 12

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

13. Find duplicate characters in a string (nested-loop version) 13

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

13. Find duplicate characters in a string (built-in version) 13

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

14. Count frequency of each character in a string 14

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

15. Check whether two strings are anagrams 14

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

16. Find the longest word in a sentence 15

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

17. Find the shortest word in a sentence 16

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

C# String Practice Reference Page 4


18. Reverse each word in a sentence 16

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

19. Check whether a string contains only digits 16

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

20. Check whether a string contains only alphabets 17

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

21. Compress a string (run-length style) 17

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

22. Find the second most repeated character 18

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

23. Remove special characters from a string 19

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

24. Find all substrings of a string 19

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

C# String Practice Reference Page 5


25. Check whether a string is a rotation of another string 19

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

26. Count number of uppercase and lowercase letters separately 20

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

27. Replace multiple spaces with a single space 20

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

28. Find longest substring without repeating characters 21

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Additional reference snippets from the source 21

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Breakdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Extra example or note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

C# String Practice Reference Page 6


String basics and built-in methods
Source code
string name = "SHEJIN JOSE";

[Link](name);
[Link]([Link]);
[Link]([Link]());
[Link]([Link]());

string name2 = " SHE JIN ";

[Link]("--" + [Link]() + "--");


[Link]("--" + [Link]() + "--");
[Link]("--" + [Link]() + "--");

[Link]([Link]("N")); // checks whether a string contains a


substring/character.

[Link]([Link]("S"));
[Link]([Link]('N'));
[Link]([Link]("H"));
[Link]([Link]("J"));
[Link]([Link](3,4)); // Substring(startIndex, length)

[Link]([Link]("JOSE","NOTHING TO DISPLAY"));

String data = "Ammu,Python,Java,C#";


string[] arr = [Link](',');
foreach(String s in arr)
{
[Link](s);
}

string lname = "perera";


[Link]([Link](name," ",lname));
[Link]($"MY NAME IS {name}");

string n = "ji";
[Link]([Link](n));

Breakdown
• This opening block demonstrates common string APIs in C#: Length, ToUpper, ToLower, Trim, Contains,
StartsWith, EndsWith, IndexOf, LastIndexOf, Substring, Replace, Split, Concat, interpolated strings, and
IsNullOrEmpty.
• A useful improvement: prefer StringBuilder when repeatedly building a string in loops, and use
[Link] / [Link] for cleaner character checks in real projects.

Extra example or note


Example: string name = "Ajay"; [Link]([Link]());

1. Reverse a given string


Source code
string str1 = "HELLO WORLD";
for (int i = [Link] - 1; i >= 0; i--)
{
[Link](str1[i]);
}

Breakdown
• The loop walks from the last index to the first and prints each character.

C# String Practice Reference Page 7


• Complexity: O(n) time and O(1) extra space.

Extra example or note


Example: input 'CSharp' -> output 'prahSC'.

2. Check whether a string is a palindrome


Source code
string str1 = "MALAYALAM";
string str2 = "";
for (int i = [Link] - 1; i >= 0; i--)
{
str2 += str1[i];
}

if (str1 == str2)
{
[Link]($"{str1} is a palindrome");
}
else
{
[Link]($"{str1} is not a palindrome");
}

Breakdown
• The code creates a reversed copy and compares it with the original string.
• A faster version would use two pointers or reverse only once with StringBuilder.

Extra example or note


Example: 'level' is a palindrome, 'hello' is not.

3. Count the number of vowels in a string


Source code
string str3 = "HI I AM SHEJIN JOSE AKA SJP";
string vow = "AEIOUaeiou";
int count = 0;
foreach(char i in str3)
{
if ([Link](i))
{
count++;
}
}
[Link]($"[Link] VOWELS :{count}");

Breakdown
• The code checks each character against a vowel lookup string.
• Spaces and punctuation are ignored automatically because they are not vowels.

Extra example or note


Example: 'OpenAI' has 4 vowels.

4. Count the number of consonants in a string


Source code

C# String Practice Reference Page 8


string str3 = "HI I AM SHEJIN JOSE AKA SJP";
string vow = "AEIOUaeiou";
int count = 0;
foreach (char i in str3)
{
if (![Link](i))
{
count++;
}
}
[Link]($"[Link] CONSONANTS :{count}");

Breakdown
• This logic counts every character that is not a vowel, including spaces.
• For a strictly correct consonant count, first check that the character is a letter.

Extra example or note


Example improvement: count only letters that are not vowels.

5. Count the number of characters in the string


Source code
string str5 = "HI I AM SHEJIN JOSE AKA SJP";
int count3 = 0;
foreach(char i in str5)
{
if( i !=' ')
{
count3++;
}
}
[Link]($"[Link] LETTERS={count3}");

Breakdown
• The source counts non-space characters, so it is really counting letters/visible characters rather than all
characters.
• If you need total characters including spaces, use [Link].

Extra example or note


Example: 'A B' -> non-space count is 2, total length is 3.

6. Count the number of words in the string


Source code
string str4 = "HI I AM SHEJIN JOSE AKA SJP";
string[] arr = [Link](" ");
int count2 = 0;
foreach(string word in arr)
{
count2++;
}
[Link]($"[Link] WORDS={count2}");

Breakdown
• The logic splits on spaces and counts array elements.
• Note: multiple spaces can create empty entries; using Split(' ', [Link])
is safer.

C# String Practice Reference Page 9


Extra example or note
Example: 'one two three' -> 3 words.

7. Convert a string to lowercase without using built-in method


Source code
string str5 = "HI I AM SHEJIN JOSE AKA SJP";
string lower = "";

foreach (char i in str5)


{
if (i >= 'A' && i <= 'Z')
{
int low = i + 32;
lower += (char)low;
}
else
{
lower += i;
}
}
[Link](lower);

Breakdown
• The ASCII trick adds 32 to convert uppercase letters to lowercase.
• This works for basic English letters but not for all Unicode characters.

Extra example or note


Example: 'ABC xyz' -> 'abc xyz'.

8. Convert a string to uppercase without using built-in method


Source code
string str6 = "hi i am shejin jose aka sjp";
string upper = "";
foreach (char i in str6)
{
if (i >= 'a' && i <= 'z')
{
int up = i - 32;
upper += (char)up;
}
else
{
upper += i;
}
}
[Link](upper);

Breakdown
• This is the reverse of the previous ASCII conversion.
• Again, it is limited to basic ASCII lowercase letters.

Extra example or note


Example: 'hello' -> 'HELLO'.

C# String Practice Reference Page 10


9. Remove all spaces between the string
Source code
string str6 = "hi i am shejin jose aka sjp";
string nospace = [Link](" ", "");
[Link](nospace);

Breakdown
• Replace(' ', '') removes all spaces.
• If you need to remove all whitespace characters, use a regex or a char-based loop.

Extra example or note


Example: 'a b c' -> 'abc'.

10. Replace all occurrences of a character with another character


Source code
string str6 = "hi i am shejin jose aka sjp";
[Link]("ENTER THE LETTER TO REPLACE:");
char char1 = [Link]([Link]());
[Link]("ENTER THE LETTER TO REPLACE WITH:");
char char2 = [Link]([Link]());

string rewrite = [Link](char1, char2);


[Link](rewrite);

Breakdown
• The user enters two characters, and Replace substitutes every occurrence of the first with the second.
• This is a simple interactive transformation.

Extra example or note


Example: replace 'a' with '@' in 'banana' -> 'b@n@n@'.

11. Remove duplicate characters from a string


Source code
string str7 = "hi i am shejin jose aka sjp";
string nodup = "";

foreach (char ch in str7)


{
if (![Link](ch))
{
nodup += ch;
}
}
[Link](nodup);

Breakdown
• Characters are appended only the first time they appear.
• The order of first appearance is preserved.

Extra example or note


Example: 'programming' -> 'progamin' (depending on spaces and input).

C# String Practice Reference Page 11


12. Find the first non-repeated character in a string (nested-loop
version)
Source code
string str8 = "hi i am shejin jose aka sjp";

for (int i = 0; i < [Link]; i++)


{
int count = 0;

for (int j = 0; j < [Link]; j++)


{
if (i == j)
{
continue;
}
else
{
if (str8[i] == str8[j])
{
count++;
}
}
}
if (count == 0)
{
[Link](str8[i]);
break;
}
}

Breakdown
• The outer loop picks each character, and the inner loop checks whether it appears elsewhere.
• The first character with zero matches is printed.

Extra example or note


Example: for 'swiss', the first non-repeated character is 'w'.

12. Find the first non-repeated character in a string (built-in


version)
Source code
string str8 = "hi i am shejin jose aka sjp";

foreach(char ch in str8)
{
if ([Link](ch) == [Link](ch))
{
[Link](ch);
break;
}
}

Breakdown
• IndexOf and LastIndexOf help detect whether the character occurs only once.
• This is shorter and easier to read than the nested-loop version.

Extra example or note


Example: 'abacddbe' -> first unique character is 'c'.

C# String Practice Reference Page 12


13. Find duplicate characters in a string (nested-loop version)
Source code
string str8 = "hi i am shejin jose aka sjp";
string dupval = "";

for (int i = 0; i < [Link]; i++)


{
for (int j = 0; j < [Link]; j++)
{
if (i == j)
{
continue;
}
else
{
if (str8[i] == str8[j])
{
if (![Link](str8[i]))
{
dupval += str8[i];
break;
}
}
}
}
}
[Link](dupval);

Breakdown
• The code searches for repeated characters and stores each duplicate once.
• Because it uses nested loops, it is O(n^2).

Extra example or note


Example: 'success' -> duplicates include 's' and 'c'.

13. Find duplicate characters in a string (built-in version)


Source code
string str8 = "hi i am shejin jose aka sjp";
string dupval = "";

foreach (char ch in str8)


{
if ([Link](ch) != [Link](ch))
{
if (![Link](ch)) {
dupval += ch;
}
}
}
[Link](dupval);

Breakdown
• A character is duplicate when its first and last positions are different.
• The inner duplicate guard prevents repeated printing of the same character.

Extra example or note


Example: for 'balloon', duplicates are 'l' and 'o'.

C# String Practice Reference Page 13


14. Count frequency of each character in a string
Source code
string str10 = "hi i am shejin jose";
string dupe = "";

foreach (char ch in str10)


{
if ([Link](ch))
{
continue;
}

int count = 0;
foreach (char ch2 in str10)
{
if (ch == ch2)
{
count++;
}
}
[Link]($"[Link]{ch} IN '{str10}' : {count}");
dupe += ch;
}

Breakdown
• The outer loop selects each unseen character and the inner loop counts matches.
• The dupe string prevents the same frequency from being printed repeatedly.

Extra example or note


Example: in 'aab', frequency of 'a' is 2 and 'b' is 1.

15. Check whether two strings are anagrams


Source code
string str1 = "hello orld";
string str2 = "hello owrd";
string str3 = "";

bool anag = true;

if ([Link] == [Link])
{
foreach (char ch in str1)
{
if ([Link](ch))
{
continue;
}

int count1 = 0;
foreach (char ch1 in str1)
{
if (ch1 == ch)
{
count1++;
}
}
int count2 = 0;

foreach (char ch3 in str2)


{
if (ch == ch3)
{

C# String Practice Reference Page 14


count2++;
}
}

if (count1 == count2)
{
anag = true;
}
else
{
anag = false;
break;
}
str3 += ch;
}
if (anag == false)
{
[Link]($"{str1} and {str2} are not anagrams");
}
else
{
[Link]($"{str1} and {str2} are anagrams");
}
}
else
{
[Link]($"{str1} and {str2} are not anagrams");
}

Breakdown
• Two strings are anagrams if they have the same length and the same character counts.
• The algorithm compares frequencies character by character.

Extra example or note


Example: 'listen' and 'silent' are anagrams.

16. Find the longest word in a sentence


Source code
string str11 = "hi i am shejin jose";
int len = 0;
string long_word = "";

string[] words = [Link](' ');


foreach(string word in words)
{
if (len < [Link])
{
len = [Link];
long_word = word;
}
}
[Link]($"LONGEST WORD ={long_word}");

Breakdown
• The sentence is split into words, and the longest word is tracked with a running maximum.
• If there are ties, the first longest word is retained.

Extra example or note


Example: in 'I love programming', the longest word is 'programming'.

C# String Practice Reference Page 15


17. Find the shortest word in a sentence
Source code
string str11 = "hi i am shejin jose";
int len = 1000;
string short_word = "";

string[] words = [Link](' ');


foreach(string word in words)
{
if (len > [Link])
{
len = [Link];
short_word = word;
}
}
[Link]($"LONGEST WORD ={short_word}");

Breakdown
• This is the same idea as the previous problem, but tracking the minimum length.
• The output label in the source says LONGEST WORD, but the variable stores the shortest word.

Extra example or note


Example: in 'I love coding', the shortest word is 'I'.

18. Reverse each word in a sentence


Source code
string str12 = "hi i am shejin jose";

for (int i = [Link] - 1; i >= 0; i--)


{
[Link](str12[i]);
}

Breakdown
• The source label says 'reverse each word', but the code actually reverses the whole sentence.
• To reverse each word separately, split on spaces and reverse each token.

Extra example or note


Example correction: 'hi am' -> 'ih ma'.

19. Check whether a string contains only digits


Source code
string str13 = "12334353636365363553635636";
string number = "0123456789";
bool val = false;

foreach(char ch in str13)
{
if (![Link](ch)) {
val = true;
break;
}
}

if (val)

C# String Practice Reference Page 16


{
[Link]("I CONTAIN ALPHABETS");
}
else
{
[Link]("I CONTAIN DIGITS ONLY");
}

Breakdown
• The logic checks that every character appears in the digit lookup string.
• The printed message 'I CONTAIN ALPHABETS' is a little misleading; it really means 'contains
non-digits'.

Extra example or note


Example: '1234' passes, '12a4' fails.

20. Check whether a string contains only alphabets


Source code
string str14 = "hi i am shejin jose";
string number = "0123456789";
bool val = false;

foreach (char ch in str14)


{
if ([Link](ch))
{
val = true;
break;
}
}

if (val)
{
[Link]("I CONTAIN DIGITS");
}
else
{
[Link]("I CONTAIN ALPHABETS ONLY");
}

Breakdown
• This checks for the presence of digits, not whether the string is made only of alphabets.
• A cleaner solution is to verify each character with [Link].

Extra example or note


Example: 'abcXYZ' passes, 'abc1' fails.

21. Compress a string (run-length style)


Source code
string str15 = "aabczzaaaaxxedfseaadddderr";
string compressed = "";

for (int i = 0; i < [Link];)


{
char currentChar = str15[i];
int count = 0;

while (i < [Link] && str15[i] == currentChar)

C# String Practice Reference Page 17


{
count++;
i++;
}

compressed += $"{currentChar}{count}";
}

[Link](compressed);

Breakdown
• This compresses consecutive repeated characters into character-count pairs.
• It is run-length encoding for grouped characters.

Extra example or note


Example: 'aaabb' -> 'a3b2'.

22. Find the second most repeated character


Source code
string str16 = "aabczzaaaaxxedfseaadddderr";
string dupe = "";
int max = 0, second_max = 0;
char maxChar = ' ', secondChar = ' ';

foreach (char ch in str16)


{
if ([Link](ch))
{
continue;
}

int count = 0;
foreach (char ch2 in str16)
{
if (ch == ch2)
{
count++;
}
}
if (count > max)
{
second_max = max;
secondChar = maxChar;

max = count;
maxChar = ch;
}
else if (count > second_max && count < max)
{
second_max = count;
secondChar = ch;
}

[Link]($"[Link]{ch} IN '{str16}' : {count}");


dupe += ch;
}
[Link]($"SECOND MOST REPETED CHARATER IS {secondChar}");

Breakdown
• The algorithm tracks the highest and second-highest frequency counts while iterating.
• The output identifies the character with the second largest frequency.

Extra example or note

C# String Practice Reference Page 18


Example: in 'aabbbcccc', the second most repeated character is 'b'.

23. Remove special characters from a string


Source code
string str17 = "Hello WORLD! #I AM HAPPY#";
string specal = "!@#$%^&*()<>?/";
string nospecial = "";
foreach(char ch in str17)
{
if (![Link](ch))
{
nospecial += ch;
}
}
[Link](nospecial);

Breakdown
• The code preserves characters that are not in the special-character lookup string.
• This removes only the listed symbols, not every possible punctuation mark.

Extra example or note


Example: 'Hi, there!' would still keep the comma unless it is added to the lookup set.

24. Find all substrings of a string


Source code
string str18 = "abcd";

for (int i = 0; i < [Link]; i++)


{
for (int j = i + 1; j <= [Link]; j++)
{
[Link]([Link](i, j - i));
}
}

Breakdown
• The nested loops enumerate every start and end position pair.
• This prints all contiguous substrings, not permutations.

Extra example or note


Example: 'abc' -> 'a', 'ab', 'abc', 'b', 'bc', 'c'.

25. Check whether a string is a rotation of another string


Source code
string str19 = "HELLOWORLD";
string str20 = "WORLDHELLO";

if ([Link] == [Link])
{
str19 = str19 + str19;
[Link](str19);

if ([Link](str20))

C# String Practice Reference Page 19


{
[Link]("IT IS A ROTATION OF THE STRING.");
}
else
{
[Link]("IT IS NOT A ROTATION OF THE STRING.");
}
}
else
{
[Link]("IT IS NOT A ROTATION OF A STRING.");
}

Breakdown
• If two strings have the same length, one string is rotated by checking whether it appears inside the
doubled version of the other.
• This is a classic and efficient rotation check.

Extra example or note


Example: 'ABCD' and 'CDAB' are rotations.

26. Count number of uppercase and lowercase letters separately


Source code
string str20 = "Hi Hello I am Jithin";
int upper = 0;
int lower = 0;

foreach (char i in str20)


{
if (i >= 'A' && i <= 'Z')
{
upper++;
}
else if (i >= 'a' && i <= 'z')
{
lower++;
}
}

[Link]($"[Link] UPPER CASE = {upper}");


[Link]($"[Link] LOWER CASE = {lower}");

Breakdown
• The loop classifies each character by ASCII range.
• Spaces are ignored because they are neither uppercase nor lowercase letters.

Extra example or note


Example: 'AbC' -> uppercase 2, lowercase 1.

27. Replace multiple spaces with a single space


Source code
using [Link];

string str21 = "This is a string with multiple spaces.";


string result = [Link](str21, @"\s+", " ");

[Link](result);

C# String Practice Reference Page 20


Breakdown
• Regex replaces runs of whitespace with one single space.
• The source also includes a manual version that removes consecutive spaces by scanning character by
character.

Extra example or note


Example manual idea: collapse repeated ' ' characters while preserving word order.

28. Find longest substring without repeating characters


Source code
string str22 = "helloqwert";
string longest = "";

for (int i = 0; i < [Link]; i++)


{
string current = "";

for (int j = i; j < [Link]; j++)


{
if (![Link](str22[j]))
{
current += str22[j];

if ([Link] > [Link])


{
longest = current;
}
}
else
{
break;
}
}
}

[Link]("Longest substring: " + longest);


[Link]("Length: " + [Link]);

Breakdown
• The inner loop grows a substring until a repeated character appears, then stops.
• This produces the longest substring without repeated characters using a brute-force approach.

Extra example or note


Example: 'abcabcbb' -> longest unique substring could be 'abc'.

Additional reference snippets from the source


Source code
string str = "dcba";

char[] arr = [Link]();


[Link](arr);

string sorted = new string(arr);

[Link]("Sorted string: " + sorted);

string str18 = "ABCD"; // Find all permutations of a string (reference only in the source)

C# String Practice Reference Page 21


Breakdown
• The source ends with two extra reference topics: sorting characters alphabetically and permutations of a
string.
• The permutations topic appears as a heading in the file, but the implementation is not shown.

Extra example or note


Example: sorted 'dcba' becomes 'abcd'.

C# String Practice Reference Page 22

You might also like