w3resource logo
C# Sharp Exercises

C# Sharp programming Exercises, Practice, Solution : String

C# Sharp String [43 exercises with solution]

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 StringComparison 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 CompareOrdinal 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

C# Sharp Code Editor:

More to Come !