IKH

Practice Question

Question

Write a program to reverse the given String

1st Way

Python
s=input("Enter Some String:")
print(s[::-1])

Output

PowerShell
Enter Some String:ajay
yaja

2nd Way

Python
s=input("Enter Some String:")
print(''.join(reversed(s)))

Output

PowerShell
Enter Some String:ajay
yaja

3rd Way

Python
s=input("Enter Some String:")
i=len(s)-1
target=''
while i>=0:
    target=target+s[i]
    i=i-1
print(target)

Output

PowerShell
Enter Some String:ajay
yaja

Question

Program to reverse order of words.

Python
s=input("Enter Some String:")
l=s.split()
l1=[]
i=len(l)-1
while i>=0:
    l1.append(l[i])
    i=i-1
output=' '.join(l1)
print(output)

Output

PowerShell
Enter Some String:learning python is very easy
easy very is python learning

Question

Program to reverse internal content of each word.

Python
s=input("Enter Some String:")
l=s.split()
l1=[]
i=0
while i<len(l):
    l1.append(l[i][::-1])
    i=i+1
output=' '.join(l1)
print(output)

Output

PowerShell
Enter Some String:ajay shukla
yaja alkuhs

Question

Write a program to print characters at odd position and even position for the given String?

1st Way

Python
s=input("Enter Some String:")
print("Characters at Even Position:",s[0::2])
print("Characters at Odd Position:",s[1::2])

Output

Python
Enter Some String:python
Characters at Even Position: pto
Characters at Odd Position: yhn

2nd Way

Python
s=input("Enter Some String:") 
i=0 
print("Characters at Even Position:") 
while i< len(s): 
    print(s[i],end=',') 
    i=i+2 
print() 
print("Characters at Odd Position:") 
i=1 
while i< len(s): 
    print(s[i],end=',') 
    i=i+2

Output

PowerShell
Enter Some String:python
Characters at Even Position:
p,t,o,
Characters at Odd Position:
y,h,n,

Question

Program to merge characters of two strings into a single string by taking characters alternatively.

Python
s1=input("Enter First String:")
s2=input("Enter Second String:")
output=''
i,j=0,0
while i<len(s1) or j<len(s2):
    if i<len(s1):
        output=output+s1[i]
        i+=1
    if j<len(s2):
        output=output+s2[j]
        j+=1
print(output)

Output

PowerShell
Enter First String:internet
Enter Second String:hub
ihnutbernet

Question

Write a program to sort the characters of the string and first alphabet symbols followed by numeric values.
Python
s=input("Enter Some String:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
for x in sorted(s1):
    output=output+x
for x in sorted(s2):
    output=output+x
print(output)

Output

PowerShell
Enter Some String:B4A1D3
ABD134

Question

Write a program for the following requirement

  • input: a4b3c2
  • output: aaaabbbcc
Python
s=input("Enter Some String:")
output=''
for x in s:
  if x.isalpha():
    output=output+x
    previous=x
  else:
    output=output+previous*(int(x)-1)
print(output)

Output

PowerShell
Enter Some String:a4b3c2
aaaabbbcc

Question

Write a program to perform the following activity

  • input: a4k3b2
  • output: aeknbd
PowerShell
s=input("Enter Some String:")
output=''
for x in s:
  if x.isalpha(): 
    output=output+x 
    previous=x 
  else: 
    output=output+chr(ord(previous)+int(x)) 
print(output)

Output

PowerShell
Enter Some String:a4k3b2
aeknbd

Question

Write a program to remove duplicate characters from the given input string?

  • input: ABCDABBCDABBBCCCDDEEEF
  • output: ABCDEF
Python
s=input("Enter Some String:")
l=[]
for x in s:
  if x not in l:
    l.append(x)
output=''.join(l)
print(output)

Output

PowerShell
Enter Some String:ABCDABBCDABBBCCCDDEEEF
ABCDEF

Question

Write a program to find the number of occurrences of each character present in the given String?

  • input: ABCABCABBCDE
  • output: A-3,B-4,C-3,D-1,E-1
Python
s=input("Enter the Some String:")
d={}
for x in s:
  if x in d.keys():
    d[x]=d[x]+1
  else:
    d[x]=1
for k,v in d.items():
  print("{} = {} Times".format(k,v))

Output

PowerShell
Enter the Some String:ABCABCABBCDE
A = 3 Times
B = 4 Times
C = 3 Times
D = 1 Times
E = 1 Times

Report an error