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.
In the table given below, we have
listed out various string programs.
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
Output:
Length of string: 8
Click to view string
code
|
2.
|
Source code to compare two string
Enter
a string1: liveviar
Enter
a string2: liveviar
Output:
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
Output:
Strings are not equal.
String1 doesnot match at index: 6, i.e., i
Case2:
Enter String1: liveviar
Enter String2: imaginationhunt
Output:
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Case3:
Enter String1: liveviar
Enter String2: liveviar
Output:
Strings are equal.
Click to view string
code
|
4.
|
Source code to reverse string
Enter
string: manish gautam
Output:
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
Output:
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
Output:
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
Output:
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
Output:
ReferenceEquals: False
Equals: True
Using in-build
Clone() function
str1 = Imaginationhunt
str2 = Imaginationhunt
Output:
ReferenceEquals: True
Equals: True
Using Assignment
'=' operator
str1 = Imaginationhunt
str2 = Imaginationhunt
Output:
ReferenceEquals: True
Equals: True
Click to view string
code
|
10.
|
Source code to count characters and its
category
Enter
string: Liveviar 23@#$
Output:
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
Output:
String1 contain searched string
Click to view string
code
|
12.
|
Source code to validate whether Password contain same 3(three) characters
which exist in UserId
Case-1:
Password is not same
Enter
UserId: easytrip
Enter
Password: hjabcfhj
Output:
Password
accepted
Case-2:
When password contain similar 3 characters that exist in UserId
Enter
UserId: ghabcdkhabcdjabcf
Enter
Password: hjabcfhj
Output:
Invalid!
Password contain same 3 three characters which exist in UserId
Click
to view string
code
|
13.
|
Source code to
check all characters in string 2 is contained in string1
Case-1:
Enter string1: imagination
Enter string2: namingition
Output:
String2 contain all characters of string1
Case-2:
Enter string1: imagination
Enter string2: sarcastical
Output:
String2 doesnot contain all characters of string1
Case-3:
Enter string1: liveviar
Enter string2: rivale
Output:
String2 doesnot contain all characters of string1
Click to view string
code
|
14.
|
Source code to create string contain
atleast 2 alphabet, 2 number, 2 special length, min 8 using regex
Enter
String:P4%_$23aTsU/
Output:
Perfect
string matching all criteria
Click
to view string
code
|
15.
|
Source code to
First reverse the string than make the even position character to upper case
Enter String: imagination
Output:
Reverse String: noitanigami
Reverse String: NoItAnIgAm
Click to view string
code
|
16.
|
Source code to First reverse the string
than make the even position character to upper case using ternary operator
Enter
String: imagination
Output:
Reverse
String: noitanigami
Reverse
String: NoItAnIgAm
Click
to view string
code
|
For any
query, comment us below.
Previous – Formula based program
Keep learning and sharing...
No comments:
Post a Comment