Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp programming Exercises, Practice, Solution : String

C# Sharp String [56 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a program in C# Sharp to input a string and print it. Go to the editor
Test Data :
Input the string : Welcome, w3resource
Expected Output :

The string you entered is : Welcome, w3resource 

Click me to see the solution

2. Write a program in C# Sharp to find the length of a string without using library function. Go to the editor
Test Data :
Input the string : w3resource.com
Expected Output :

Length of the string is : 15 

Click me to see the solution

3. Write a program in C# Sharp to separate the individual characters from a string. Go to the editor
Test Data :
Input the string : w3resource.com
Expected Output :

The characters of the string are : 
w  3  r  e  s  o  u  r  c  e  .  c  o  m 

Click me to see the solution

4. Write a program in C# Sharp to print individual characters of the string in reverse order. Go to the editor
Test Data :
Input the string : w3resource.com
Expected Output :

The characters of the string in reverse are : 

m  o  c  .  e  c  r  u  o  s  e  r  3  w 
 

Click me to see the solution

5. Write a program in C# Sharp to count the total number of words in a string. Go to the editor
Test Data :
Input the string : This is w3resource.com
Expected Output :

Total number of words in the string is : 3 

Click me to see the solution

6. Write a program in C# Sharp to compare two string without using string library functions. Go to the editor
Test Data :
Input the 1st string : This is first string
Input the 2nd string : This is first string
Expected Output :

The length of both strings are equal and 
also, both strings are equal.

Click me to see the solution

7. Write a program in C# Sharp to count a total number of alphabets, digits and special characters in a string. Go to the editor
Test Data :
Input the string : Welcome to w3resource.com
Expected Output :

Number of Alphabets in the string is : 21 
Number of Digits in the string is : 1 
Number of Special characters in the string is : 4 

Click me to see the solution

8. Write a program in C# Sharp to copy one string to another string. Go to the editor
Test Data :
Input the string : This is a string to be copied.
Expected Output :

The First string is : This is a string to be copied. 

The Second string is : This is a string to be copied. 

Number of characters copied : 31 

Click me to see the solution

9. Write a program in C# Sharp to count a total number of vowel or consonant in a string. Go to the editor
Test Data :
Input the string : Welcome to w3resource.com
Expected Output :

The total number of vowel in the string is : 9 
The total number of consonant in the string is : 12 

Click me to see the solution

10. Write a program in C# Sharp to find maximum occurring character in a string. Go to the editor
Test Data :
Input the string : Welcome to w3resource.com.
Expected Output :

The Highest frequency of character 'e' 
appears number of times : 4 

Click me to see the solution

11. Write a program in C# Sharp to sort a string array in ascending order. Go to the editor
Test Data :
Input the string : this is a string
Expected Output :

After sorting the string appears like : 
a g h i i i n r s s s t t 

Click me to see the solution

12. Write a program in C# Sharp to read a string through the keyboard and sort it using bubble sort. Go to the editor
Test Data :
Input number of strings :3
Input 3 strings below :
abcd
zxcv
mnop
Expected Output :

After sorting the array appears like : 
abcd 
mnop 
zxcv

Click me to see the solution

13. Write a program in C# Sharp to extract a substring from a given string without using the library function. Go to the editor
Test Data :
Input the string : This is a test string
Input the position to start extraction :5
Input the length of substring :5
Expected Output :

The substring retrieve from the string is :  is a

Click me to see the solution

14. Write a C# Sharp program to check whether a given substring is present in the given string. Go to the editor
Test Data :
Input the string : This is a Test String
Input the substring to search : Test
Expected Output :

The substring exists in the string
Click me to see the solution

15. Write a program in C# Sharp to read a sentence and replace lowercase characters by uppercase and vice-versa. Go to the editor
Test Data :
Input the string : This is a string
Expected Output :

After conversion, the string is : tHIS IS A STRING 

Click me to see the solution

16. Write a program in C# Sharp to check the username and password. Go to the editor
Test Data :
Input a username: uesr
Input a password: pass
Input a username: abcd
Input a password: 1234
Expected Output :

Password entered successfully!

Click me to see the solution

17. Write a program in C# Sharp to search the position of a substring within a string. Go to the editor
Test Data :
Input a String: this is a string
Input a substring to be found in the string: is
Expected Output :

Found 'is' in 'this is a string' at position 2

Click me to see the solution

18. Write a program in C# Sharp to check whether a character is an alphabet and not and if so, go to check for the case. Go to the editor
Test Data :
Input a character: Z

Expected Output :

The character is uppercase.

Click me to see the solution

19. Write a program in C# Sharp to find the number of times a substring appears in a given string. Go to the editor
Test Data :
Input the original string : this is original string
Input the string to be searched for : str
Expected Output :

The string 'str' occurs 1 times

Click me to see the solution

20. Write a program in C# Sharp to insert a substring before the first occurrence of a string. Go to the editor
Test Data :
Input the original string : this is a string
Input the string to be searched for : a
Input the string to be inserted : test
Expected Output :

The modified string is : this is  test a string

Click me to see the solution

21. Write a C# Sharp program to compare (less than, greater than, equal to ) two substrings. Go to the editor
Expected Output :

str1 = 'computer', str2 = 'system'                                               
Substring 'mp' in 'computer' is less than substring 'sy' in 'system'.  

Click me to see the solution

22. Write a C# Sharp program to compare two substrings that only differ in case. The first comparison ignores case and the second comparison considers case. Go to the editor
Expected Output :

str1 = 'COMPUTER', str2 = 'computer'                                             
Ignore case:                                                                     
Substring 'MP' in 'COMPUTER' is equal to substring 'mp' in 'compu                                                                              
Honor case:                                                                      
Substring 'MP' in 'COMPUTER' is greater than substring 'mp' in 'computer'.   

Click me to see the solution

23. Write a C# Sharp program to compare two substrings using different cultures and ignoring the case of the substrings. Go to the editor
Expected Output :

str1 = 'COMPUTER', str2 = 'computer'                                             
Ignore case, Turkish culture:                                                    
Substring 'UT' in 'COMPUTER' is equal to substring 'ut' in 'computer'.           
                                                                                 
Ignore case, invariant culture:                                                  
Substring 'UT' in 'COMPUTER' is equal to substring 'ut' in 'computer'.   

Click me to see the solution

24. Write a C# Sharp program to compare the last names of two people. It then lists them in alphabetical order. Go to the editor
Expected Output :

Sorted alphabetically by last name:                                              
Michel Jhonson                                                                   
John Peterson  

Click me to see the solution

25. Write a C# Sharp program to compare four sets of words by using each member of the string comparison enumeration. The comparisons use the conventions of the English (United States) and Sami (Upper Sweden) cultures.
Note : The strings "encyclopedia" and "encyclopedia" are considered equivalent in the en-US culture but not in the Sami (Northern Sweden) culture. Go to the editor

Expected Output :

   case = Case (CurrentCulture): False                                           
   case = Case (CurrentCultureIgnoreCase): True                                  
   case = Case (InvariantCulture): False                                         
   case = Case (InvariantCultureIgnoreCase): True                                
   case = Case (Ordinal): False                                                  
   case = Case (OrdinalIgnoreCase): True                                         
........ 

Click me to see the solution

26. Write C# Sharp program to demonstrate that the Compare(String, String, Boolean) method is equivalent to using ToUpper or ToLower when comparing strings. Go to the editor
Expected Output :

Comparing 'QRS' and 'qrs':                                                       
The Strings are equal when capitalized? true                                     
The Strings are equal when case is ignored? true

Click me to see the solution

27. Write a C# Sharp program to demonstrate how culture can affect a comparison. Go to the editor

Note : In Czech – Czech Republic culture, "ch" is a single character that is greater than "d". However, in English - United States culture, "ch" consists of two characters, and "c" is less than "d".

Expected Output :

For en-US: change < dollar                                                       
For cs-CZ: change > dollar

Click me to see the solution

28. Write a C# Sharp program to compare two strings in following three different ways produce three different results. Go to the editor

a. using linguistic comparison for the en-US culture;
b. using linguistic case-sensitive comparison for the en-US culture;
c. using an ordinal comparison. It illustrates how the three methods of comparison

Expected Output :

'sister' comes before 'Sister'.                                                  
'sister' is the same as 'Sister'.                                                
'sister' comes after 'Sister'.

Click me to see the solution

29. Write a C# Sharp program to compare three versions of the letter "I". The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed. Go to the editor

Expected Output :

Compare three versions of the letter I using different values of StringComparison.                                                                                
The current culture is en-US.                                                    
                                                                                 
StringComparison.CurrentCulture:                                                 
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)       
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0
049)

Click me to see the solution

30. Write a C# Sharp program to demonstrate that compare ordinal and Compare use different sort orders. Go to the editor

Expected Output :

CompareOrdinal("xyz"[1], "XYZ"[1]):                                              
   'y' is greater than 'Y'                                                       
Compare("xyz"[1], "XYZ"[1]):                                                     
   'y' is less than 'Y'

Click me to see the solution

31. Write a C# Sharp program to perform and ordinal comparison of two strings that only differ in case. Go to the editor

Expected Output :

Compare the numeric values of the corresponding Char objects in each string.     
str1 = 'JAVA', str2 = 'python'                                                   
String 'JAVA' is less than String 'python'.

Click me to see the solution

32. Write a C# Sharp program to compare a given string with set of strings. Go to the editor

Expected Output :

Bad argument: TestClass (type TestClass)                                         
Comparing 'some text' with 'TestClass': -1                                       
Bad argument: 123 (type Int32)                                                   
Comparing 'some text' with '123': 1                                              
Comparing 'some text' with 'some text': 0                                        
Comparing 'some text' with 'Some Text': -1

Click me to see the solution

33. Write a C# Sharp program to compare the current string instance with another string. Go to the editor

Expected Output :

The strings occur in the same position in the sort order.                        
The first string follows the second in the sort order.                           
                                                                                 
The first string precedes the second in the sort order.                          
The first string precedes the second in the sort order.                          
The first string follows the second in the sort order.

Click me to see the solution

34. Write a C# Sharp program to concatenate three objects, objects with a variable and 3-element object array. Go to the editor

Expected Output :

Search for the target string "Å" in the string "abcЙࠉ".                          
                                                                                 
Using the English (United Kingdom) - "en-GB" culture:                            
Case sensitive:                                                                  
  The string to search ends with the target string: False 
  .........

Click me to see the solution

35. Write a C# Sharp program to concatenate a list of variable parameters. Go to the editor

Expected Output :

abcd --> cadb                                                                    
efgh --> hgef                                                                    
ijkl --> lkij                                                                    
mnop --> nmpo

Click me to see the solution

36. Write a C# Sharp program to concatenate three strings and display the result. Go to the editor

Expected Output :

Don't count your chickens, before the eggs, have hatched.

Click me to see the solution

37. Write a C# Sharp program to concatenate the array values of strings. Go to the editor

Expected Output :

hello welcome to C# Sharp create Windows client applications                     
                                                                                 
hello applicationsC# Sharp client create to welcome Windows

Click me to see the solution

38. Write a C# Sharp program to determine whether the string "birds" is a substring of a familiar. Go to the editor

Note : Quotation 'two birds with one stone'.

Expected Output :

'birds' is in the string 'Kill two birds with one stone': True                   
'birds begins at character position 10

Click me to see the solution

39. Write a C# Sharp program to creates two string objects with different values. When it calls the Copy method to assign the first value to the second string, the output indicates that the strings represent different object references although their values are now equal. On the other hand, when the first string is assigned to the second string, the two strings have identical values because they represent the same object reference. Go to the editor

Expected Output :

s1 = 'JAVA'                                                                      
s2 = 'Python''

Click me to see the solution

40. Write a C# Sharp program to demonstrate the CopyTo method. Go to the editor

Expected Output :

w3resource CSharp Tutoral                                                        
w3resource Python Tutoral                                                        
w3resourcedifferentutoral

Click me to see the solution

41. Write a C# Sharp program to indicate whether each string in an array ends with a period ("."). Go to the editor

Expected Output :

'Actions speak louder than words' ends in a period: False                        
'Hello!' ends in a period: False                                                 
'Python.' ends in a period: True                                                 
'PHP.' ends in a period: True                                                    
'random' ends in a period: False

Click me to see the solution

42. Write C# Sharp program to check whether a string occurs at the end of another string. Go to the editor

Expected Output :

Search for the target string "Å" in the string "abcЙࠉ".                                                       
                                                                                                              
Using the English (United Kingdom) - "en-GB" culture:                                                         
Case sensitive:                                                                                               
  The string to search ends with the target string: False                                                     
                                                                                                              
Case insensitive:                                                                                             
  The string to search ends with the target string: False                                                     
                                                                                                                    
Using the English (Australia) - "en-AU" culture:                                                              
Case sensitive:                                                                                               
  The string to search ends with the target string: False                                                     
                                                                                                              
Case insensitive:                                                                                             
  The string to search ends with the target string: False

Click me to see the solution

43. Write a C# Sharp program to determine whether a string ends with a particular substring. Go to the editor

Note : The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

Expected Output :

Determine whether a string ends with another string, using                       
different values of StringComparison.                                          
The current culture is en-US.                                                    
                                                                                 
StringComparison.CurrentCulture:                                                 
"xyzPQR" ends with "PQR".                                                        
"xyzPQR" ends with "PQR".
.......

Click me to see the solution

44. Write a C# Sharp program to get the longest Palindromic substring from a given string. Go to the editor

From Wikipedia:
In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada".

Expected Output :

Original String: aaaaaabbbbccc
Length of the longest substring without repeating characters of the said string:
aaaaa
Original String: BDEFGAABEF
Length of the longest substring without repeating characters of the said string:
AA
Original String: Python
Length of the longest substring without repeating characters of the said string:
P
Original String: Java
Length of the longest substring without repeating characters of the said string:
av

Click me to see the solution

45. Write a C# Sharp program to reverse a given string in uppercase. Go to the editor

Expected Output :

Original string: php
Said string in uppercase: PHP
Original string: java
Said string in uppercase: AVAJ
Original string: abcd
Said string in uppercase: DCBA

Click me to see the solution

46. Write a C# Sharp program to remove duplicate characters from a given string. Go to the editor

Expected Output :

Original String: aaaaaabbbbccc
After removing duplicates characters from the said string:
abc
Original String: Python
After removing duplicates characters from the said string:
Python
Original String: Java
After removing duplicates characters from the said string:
Jav

Click me to see the solution

47. Write a C# Sharp program to find the length of the longest substring without repeating characters from a given string. Go to the editor

Expected Output :

Original String: aaaaaabbbbccc
Length of the longest substring without repeating characters of the said string:
2
Original String: BDEFGAABEF
Length of the longest substring without repeating characters of the said string:
6
Original String: Python
Length of the longest substring without repeating characters of the said string:
6
Original String: Java
Length of the longest substring without repeating characters of the said string:
3

Click me to see the solution

48. Write a C# Sharp program to reverse the case (upper->lower, lower->upper) of all the characters of given string. Go to the editor

Expected Output :

Original string: PHP
After reversing the case of all characters of the said string: php

Original string: JavaScript
After reversing the case of all characters of the said string: jAVAsCRIPT

Original string: Python 3.0
After reversing the case of all characters of the said string: pYTHON 3.0

Click me to see the solution

49. Write a C# Sharp program to find the middle character(s) of a given string. Return the middle character if the length of the string is odd and return two middle characters if the length of the string is even. Go to the editor

Expected Output :

Original string: Python
Middle character(s) of the said string: th

Original string: PHP
Middle character(s) of the said string: H

Original string: C#
Middle character(s) of the said string: C#

Click me to see the solution

50. Write a C# Sharp program to find the maximum and minimum number from a given string of numbers separated by single space. Go to the editor

Expected Output :

Original string of numbers: 3 4 8 9 0 2 1
Maximum and minimum number of the said string: 9, 0

Original string of numbers: -2 -1 0 4 10
Maximum and minimum number of the said string: 10, -2

Click me to see the solution

51. Write a C# Sharp program to check whether a given string is an “isograms” or not. Return True or False. Go to the editor

From Wikipedia,
A heterogram (from hetero-, meaning 'different', + -gram, meaning 'written') is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. The terms isogram and nonpattern word have also been used to mean the same thing.

Expected Output :

Original string: Python
Check the said string is an 'isograms' or not! True

Original string: JavaScript
Check the said string is an 'isograms' or not! False

Original string: PHP
Check the said string is an 'isograms' or not! False

Original string: C#
Check the said string is an 'isograms' or not! True

Click me to see the solution

52. Write a C# Sharp program to convert the first character of each word of a given string to uppercase. Go to the editor

Expected Output :

Original string: python exercises
After converting the first character of each word of the said string:
Python Exercises
Original string: The quick brown Fox jumps over the little lazy Dog
After converting the first character of each word of the said string:
The Quick Brown Fox Jumps Over The Little Lazy Dog

Click me to see the solution

53. Write a C# Sharp program to find the position of a specified word in a given string. Go to the editor

Sample Example:
Text: The quick brown fox jumps over the lazy dog.
Position: 1 2 3 4 5 6 7 8 9

Expected Output :

Original string: The quick brown fox jumps over the lazy dog.
Position of the word 'fox' in the said string: 4
Position of the word 'The' in the said string: 1
Position of the word 'lazy' in the said string: 8

Click me to see the solution

54. Write a C# Sharp program to alternate the case of each letter in a given string and the first letter of the said string must be uppercase. Go to the editor

Expected Output:

Original string: c# Exercises

After alternating the case of each letter of the said string:
C# ExErCiSeS

Original string: C# is used to develop web apps, desktop apps, mobile apps, games and much more.

After alternating the case of each letter of the said string:
C# Is UsEd To DeVeLoP wEb ApPs, dEsKtOp ApPs, mObIlE aPpS, GaMeS aNd MuCh MoRe.

Click me to see the solution

55. Write a C# Sharp program reverse all the words of a given string which have even length. Go to the editor

Expected Output:

Original string: C# Exercises

Reverse all the words of the said string which have even length.:
#C Exercises

Original string: C# is used to develop web apps , desktop apps , mobile apps , games and much more.

Reverse all the words of the said string which have even length.:
#C si desu ot develop web sppa , desktop sppa , elibom sppa , games and hcum more.

Click me to see the solution

56. Write a C# Sharp program to find the longest common ending between two given strings. Go to the editor

Expected Output:

Original strings: running  ruminating

Common ending between said two strings:ing

Original strings: thisisatest  testing123testing

Common ending between said two strings:

Click me to see the solution

C# Sharp Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.