String based program

In this blog, we are going to code different string based program that usually asked. Many of us get confused between loops and conditions. And it is also one of the most asked interview questions.
 
String based program

In the table given below, we have listed out various string programs.

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code these programs.

S.no.
String Program
1.     

Source code to find length of string

Enter a string: liveviar
Length of string: 8

Click to view string code

2.     

Source code to compare two string

Enter a string1: liveviar
Enter a string2: liveviar
Strings are equal.

Click to view string code

3.     

Source code to find at which position strings are not equal

Case1:
Enter String1: liveviar
Enter String2: livevlar
Strings are not equal.
String1 doesnot match at index: 6, i.e., i
String2 doesnot match at index: 6, i.e., l

Case2:
Enter String1: liveviar
Enter String2: imaginationhunt
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15

Case3:
Enter String1: liveviar
Enter String2: liveviar
Strings are equal.
Strings are equal.
Strings are equal.

Click to view string code

4.     

Source code to reverse string

Enter string: manish gautam
Reverse string using in-built function: matuag hsinam
Reverse string word by word: hsinam matuag
Reverse complete sentence: matuag hsinam

Click to view string code

5.     

Source code to input/read character

Enter any character: a
Entered character is: a

Click to view string code

6.     

Source code to count frequency of character in string

Enter string: Imagination hunt
Enter any character from string to count frequency: n
Repetition of n is: 3 times

Click to view string code

7.     

8.     

Source code to combine two strings

Enter String1: imagination
Enter String2: hunt
Concatenated string: imaginationhunt

Click to view string code

9.     

Source code to copy two strings

Enter string: Imaginationhunt

Using Copy() function
str1 = Imaginationhunt
str2 = Imaginationhunt
ReferenceEquals: False
Equals: True

Using in-build Clone() function
str1 = Imaginationhunt
str2 = Imaginationhunt
ReferenceEquals: True
Equals: True

Using Assignment '=' operator
str1 = Imaginationhunt
str2 = Imaginationhunt
ReferenceEquals: True
Equals: True

Click to view string code

10.   

Source code to count characters and its category

Enter string: Liveviar 23@#$
l is constants
i is vowel
v is constants
e is vowel
v is constants
i is vowel
a is vowel
r is constants
  is specialCharacter
2 is numbers
3 is numbers
@ is specialCharacter
# is specialCharacter
$ is specialCharacter

Total number of occurences: 14
Vowels: 4
Constants: 4
Numbers: 2
Special Character: 4

Click to view string code

11.   

Source code to check whether a string is contained in other string

Enter string1: This is my eLearning website www.imaginationhunt.com
Enter searched string: imaginationhunt
String1 contain searched string
String1 contain searched string

Click to view string code



For any query, comment us below.

Previous – Formula based program



Click
imagination hunt to read latest blogs.

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions