A string is a collection of chars, we can store multiple characters set into String.
The base Index of the String will be started from 0 to size-1 similar to LIST.but all elements of the String are connected with each other.
we can write String is single quote (' '),double quote (" ") and triple quote (""").
a= 'hello' ; //Single Quote String
a = "hello" //Double quote String
a= """hello""" //Triple quote String
s = "hello"
s[0]= h
s[1]= e
s[2]=l
s[3]=l
s[4]=o
Example of String Program?
we can write String is single quote (' '),double quote (" ") and triple quote (""").
a= 'hello' ; //Single Quote String
a = "hello" //Double quote String
a= """hello""" //Triple quote String
s = "hello"
s[0]= h
s[1]= e
s[2]=l
s[3]=l
s[4]=o
Example of String Program?
Q1) WAP to count the total vowel and consonant in String?
Solution:-
s = "hello"
c1=0
c2=0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
c1=c1+1
else:
c2=c2+1
print("total vowel is ",c1," total consonent is ",c2)
Q 2) How to remove all duplicates from a given string?
Q 3) How to remove characters from the first String which are present in the second String?
Solution:-
s = "hello"
c1=0
c2=0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
c1=c1+1
else:
c2=c2+1
print("total vowel is ",c1," total consonent is ",c2)
Q2) WAP to check that String is palindrome or not?
s= input("enter string") #aaaaaa
size= len(s)-1
c=1
for i in range(0,(len(s))//2):
if s[i]!=s[size-i]:
c=0
print("not pallindrom")
break
if c==1:
print("pallindrom")
Q3) WAP to find max char in String?
Solution:-
s = "hellop"
max=s[0]
for i in range(1,len(s)):
if max<s[i]:
max=s[i]
print(max)
c=1
for i in range(0,(len(s))//2):
if s[i]!=s[size-i]:
c=0
print("not pallindrom")
break
if c==1:
print("pallindrom")
Q3) WAP to find max char in String?
Solution:-
s = "hellop"
max=s[0]
for i in range(1,len(s)):
if max<s[i]:
max=s[i]
print(max)
Q)WAP to validate email?
Solution:-
s = "@abcg.mailcom"
atpos=0
dotpos=0
for i in range(0,len(s)):
if s[i]=="@" :
atpos=i
if s[i]=="." :
dotpos=i
if atpos>0 and (dotpos-atpos)>=2:
print("valid email id")
else:
print("invalid email id")
Q) WAP to validate the mobile number?
Q) WAP to validate first name and last name?
Q) WAP to provide a weak password and a Strong password?
1234 //w
12345//w
S@$1234 //Strong
123s@$123 //Strong
Q) WAP to find the largest palindrome in String?
Q) WAP to find max char in each string in a word?
Q) WAP to replace white space with an underscore?
Q) WAP to count total upper case, lower case, number, special char in String?
s = "@abcg.mailcom"
atpos=0
dotpos=0
for i in range(0,len(s)):
if s[i]=="@" :
atpos=i
if s[i]=="." :
dotpos=i
if atpos>0 and (dotpos-atpos)>=2:
print("valid email id")
else:
print("invalid email id")
Q) WAP to validate first name and last name?
Q) WAP to provide a weak password and a Strong password?
1234 //w
12345//w
S@$1234 //Strong
123s@$123 //Strong
Q) WAP to find the largest palindrome in String?
Q) WAP to find max char in each string in a word?
Q) WAP to replace white space with an underscore?
Q) WAP to count total upper case, lower case, number, special char in String?
s = "abCA@$_123"
sc=0
chkcase=0
for data in s:
if data in('@','$','_','#','!'):
sc=sc+1
if ord(data)>=65 and ord(data)<=91:
chkcase=chkcase+1
if len(s)>5 and sc>2 and chkcase>1:
print("strong password")
else:
print("weak password")
Q) WAP to reverse a string in words?s= "C CPP DS Java PHP" # C PPC SD avaJ PHP
s = s + " "
x = []
data = ''
for i in range(0,len(s)):
if s[i]!=' ':
data = data+s[i]
else:
x.append(data)
data=''
for i in range(0,len(x)):
d = x[i]
for j in range(len(d)-1,-1,-1):
print(d[j],end='')
print()
Q 2) How to remove all duplicates from a given string?
Q 3) How to remove characters from the first String which are present in the second String?
5) How to check if two strings are rotations of each other? (solution)
Write an efficient program to test if two given String is a rotation of each other or not, e.g. if the given String is "XYZ" and "ZXY" then your function should return true, but if the input is "XYZ" and "YXZ" then return false.
6) How to reverse a given String? (solution)
Write a program to reverse a String in C/Java/Python or choice of your programming language. You can write either the recursive or iterative solution. For example, if a given input is "abcd," then your function should return "dcba".
7) How to reverse String without recursion? (solution)
Same as the previous program, but you cannot use recursion, this time, you must solve this problem in C/Java/Python using iteration or loops. If you are not familiar with recursion and repetition, you can further check the Algorithms and Data Structures - Part 1 and 2 courses on Pluralsight. One of the better course to start with fundamental techniques to solve algorithms based problems.
string programming interview questions for java developers
8) How to print all permutation of a String? (solution)
Write an efficient program to print all permutations of a given String in Java/C/Python or any programming language of your choice. For example, if given input is "123" then your program should print all 6 permutations e.g. "123", "132", "213", "231", "312" and "321".
9) How to find the first non-repeating character in a given String? (solution)
Write a program in any programming language you to find the first non-repeated character in a given String, for example, if the given String is "Java" then the first non-repeated character is "J"
10) How to reverse the words in a given String sentence? (solution)
Write an efficient program in Java or C++ to reverse the words in a given String sentence. For example, if the input is "Java is best," then your program should print "best is Java". There is no restriction on preserving white space.
11) How to find the smallest substring in a given string containing all characters of another string? (solution)
Write an efficient function in C/C++ or Java to print the smallest substring in a given string containing all characters of another String. For example, if the given String is "this is a test string" and "tist", then the output should be "t stri".
12) How to check if two given String is the anagram of each other? (solution)
Write an efficient program in Java/C/C++ to check if two String is an anagram of each other. An anagram contains are of the same length and contains the same character, but in a different order, for example, "Army" and "Mary" is the anagram. Your program should return true if both Strings are the anagram, false otherwise
13) How do you check if a given String is Palindrome or not? (solution)
Write an efficient program in your favorite programming language to check if a Given a string is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"121" is a palindrome, but "123" is not. Your function should return true if given String is a palindrome, false otherwise
14) How do you convert String to an integer? (solution)
Implement atoi() like function in C/C++/Java to convert a string to an integer. Consider all possible cases e.g. positive and negative String, the presence of + or - character, etc. For example, if the given input String is "123" then your program should return 123 and if a given input is "+231" then your program should return 231.
Predefine method of String in Python:-
Method | Description |
---|---|
Python String capitalize() Converts first character to Capital Letter
Python String center() Pads string with the specified character
Python String casefold() converts to case folded strings
Python String count() returns occurrences of a substring in a string
Python String endswith() Checks if String Ends with the Specified Suffix
Python String expandtabs() Replaces Tab character With Spaces
Python String encode() returns encoded string of given string
Python String find() Returns the index of the first occurrence of a substring
Python String format() formats string into the nicer output
Python String index() Returns Index of Substring
Python String isalnum() Checks Alphanumeric Character
Python String isalpha() Checks if All Characters are Alphabets
Python String isdecimal() Checks Decimal Characters
Python String isdigit() Checks Digit Characters
Python String isidentifier() Checks for Valid Identifier
Python String islower() Checks if all Alphabets in a String are Lowercase
Python String isnumeric() Checks Numeric Characters
Python String isprintable() Checks Printable Character
Python String isspace() Checks Whitespace Characters
Python String istitle() Checks for Titlecased String
Python String isupper() returns if all characters are uppercase characters
Python String join() Returns a Concatenated String
Python String ljust() returns a left-justified string of given width
Python String rjust() returns a right-justified string of given width
Python String lower() returns the lowercased string
Python String upper() returns an uppercased string
Python String swapcase() swap uppercase characters to lowercase; vice versa
Python String lstrip() Removes Leading Characters
Python String rstrip() Removes Trailing Characters
Python String strip() Removes Both Leading and Trailing Characters
Python String partition() Returns a Tuple
Python String maketrans() returns a translation table
Python String rpartition() Returns a Tuple
Python String translate() returns mapped charactered string
Python String replace() Replaces Substring Inside
Python String rfind() Returns the Highest Index of Substring
Python String rindex() Returns Highest Index of Substring
Python String split() Splits String from Left
Python String rsplit() Splits String From Right
Python String splitlines() Splits String at Line Boundaries
Python String startswith() Checks if String Starts with the Specified String
Python String title() Returns a Title Cased String
Python String zfill() Returns a copy of The String Padded With Zeros
Python String format_map() Formats the String Using Dictionary
Python any() Checks if any Element of an Iterable is True
Python all() returns true when all elements in iterable is true
Python ascii() Returns String Containing Printable Representation
Python bool() Converts a Value to Boolean
Python bytearray() returns an array of given byte size
Python bytes() returns an immutable bytes object
Python compile() Returns a Python code object
Python complex() Creates a Complex Number
Python enumerate() Returns an Enumerate Object
Python filter() constructs iterator from elements that are true
Python float() returns floating-point number from number, string
Python input() reads and returns a line of string
Python int() returns an integer from a number or string
Python iter() returns an iterator for an object
Python len() Returns Length of an Object
Python max() returns largest element
Python min() returns smallest element
Python map() Applies Function and Returns a List
Python ord() returns Unicode code point for Unicode character
Python reversed() returns reversed iterator of a sequence
Python slice() creates a slice object specified by range()
Python sorted() returns a sorted list from a given iterable
Python sum() Add items of an Iterable
Python zip() Returns an Iterator of Tuples
s = "ssssss ram madam jjjjjjjjjj kkk ppp ram hari sdafdfsafds dzfv"
ReplyDeletes1= s+ " ";
lst=[]
data=''
for i in range(0,len(s1)):
if s1[i]!= " ":
data=data+s1[i]
else:
lst.append(data)
data=''
#print(lst)
m=0
for i in range(0,len(lst)):
s2=lst[i]
for j in range (0 ,len(s2)//2):
if s2[j]!=s2[(len(s2)-1)-j]:
break
else:
if m<len(s2):
m=len(s2)
index=i
print(lst[index])
WAP to validate the mobile number
ReplyDeletes=input("mobile number")
if len(s)==10 and s[0]!="0":
print("valid")
else:
print("inavlid")
WAP to validate first name and last name
ReplyDeletea=input("enter full name")
c=0
for i in range(0,len(a)):
if a[i].isspace():
c=c+1
if c==1 and a[0].isspace()==False:
print("valid name")
else:
print("invalid name")
WAP to provide a weak password and Strong password
ReplyDeletep=input("enter password")
d=0
u=0
l=0
s=["@","#","*","&","^","$","!"]
sc=0
for i in p:
if i in s:
sc=sc+1
if i.isdigit():
d=d+1
if i.isupper():
u=u+1
if i.islower():
l=l+1
if len(p)>7 and sc>0 and d>0 and u>0 and l>0:
print("strong password")
else:
print("weak password")
WAP to find the largest palindrome in String
ReplyDeletea=input("enter palindrome")
s=a.split(" ")
max1=""
for i in s:
if len(i)>len(max1):
max1=i
else:
continue
if max1[::-1]==max1:
print("longest palindrome is",max1)
else:
print("no")
WAP to find max char in each string in a word
ReplyDeletea=input("enter word")
m=65
for i in a:
if ord(i)>m:
m=ord(i)
print(chr(m))
WAP to replace white space with an underscore
ReplyDeletea=input("enter name")
s=""
for i in range(0,len(a)):
if a[i].isspace():
s=s+"_"
else:
s=s+a[i]
print(s)
WAP to count total upper case, lower case, number, special char in String
ReplyDeletea=input("enter word")
d=0
u=0
l=0
s=0
for i in a:
if i.isdigit():
d=d+1
elif i.isupper():
u=u+1
elif i.islower():
l=l+1
else:
s=s+1
print(d,u,l,s)
WAP to reverse a string in words
ReplyDeletea=input("enter words")
l=a.split(" ")
l=l[::-1]
s=""
for i in l:
s=s+i+" "
print(s)
#password strenght
ReplyDeletes=input("enter password")
c=0
for i in range(0,len(s)):
if ord(s[i])>=48 and ord(s[i])<=57:
c=c+1
if ord(s[i])>=65 and ord(s[i])<=90:
c=c+1
if ord(s[i])>=97 and ord(s[i])<=122:
c=c+1
if ord(s[i])>=58 and ord(s[i])<=64:
c=c+1
if c>15:
print("strong password")
if c>=10 and c<=15:
print("medium password")
if c>6 and c<10:
print("weak password")
if c<6:
print("password should be greater than 6 character")
x=input("enter mobile number")
ReplyDeletec=0
for i in range(0,len(x)):
if ord(x[i])>=48 and ord(x[i])<=57:
c=c+1
if c==10:
print("valid mobile number")
else:
print("invalid mobile no")
fn=input("enter user name")
ReplyDeleteln=input("enter your last name")
for i in range(0,len(fn)):
if (ord(fn[i])>=65 and ord(fn[i])<=90) or (ord(fn[i])>=97 and ord(fn[i])<=122):
print("valid first name")
break;
else:
print("invalid first name")
break;
for j in range(0,len(ln)):
if (ord(ln[j])>=65 and ord(ln[j])<=90) or (ord(ln[j])>=97 and ord(ln[j])<=122):
print("valid last name")
break;
else:
print("invalid last name")
break;
#max in string
ReplyDeletes=input("enter a string")
m="0"
for i in s:
if m<i:
m=i
print(m)
replace space with @
ReplyDeletex="gourav ji"
m=' '
for i in range(len(x)):
if (x[i])==' ':
m=m+'@'
else:
m=m+x[i]
print(m)
How to remove all duplicates from a given string?
ReplyDeletest="mangalam"
i=0
c=''
for data in st:
if st.index(data)==i:
c=c+data
i=i+1
print(c)
"""5) How to check if two strings are rotations of each other? (solution)
ReplyDeleteWrite an efficient program to test if two given String is a rotation of each other or not, e.g. if the given String is "XYZ" and "ZXY" then your function should return true, but if the input is "XYZ" and "YXZ" then return false.
"""
def check_rotation(str1,str2):
if str1[-1]+str1[0:len(str1)-1]==str2:
return True
else:
return False
str1="ABCDEF"
str2="FABCDE"
print(check_rotation(str1,str2))