Skip to content

Commit e81344a

Browse files
authored
Merge pull request #597 from Manasi2001/issue-596
Bad String
2 parents 5b4ea23 + 4a2e369 commit e81344a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Strings/Bad_String.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Aim: Read a string, S and print its integer value. If S cannot be converted to
3+
an integer, print Bad String.
4+
5+
'''
6+
7+
# getting the input
8+
S = input().strip()
9+
try:
10+
# if it's possible to convert the entered string into an integer then this block will execute
11+
print(int(S))
12+
except:
13+
# if it's not possible, then this block will be executed
14+
print('Bad String')
15+
16+
'''
17+
18+
COMPLEXITY:
19+
20+
Time Complexity -> O(1)
21+
Space Complexity -> O(1)
22+
23+
Sample Input 1:
24+
3
25+
Sample Output 1:
26+
3
27+
Sample Input 2:
28+
SB
29+
Sample Output 2:
30+
Bad String
31+
32+
Explaination:
33+
'3' as a string can be converted into the integer 3, whereas SB can't be
34+
converted to an integer hence the 'except' block is executed and 'Bad String'
35+
is printed.
36+
37+
'''

0 commit comments

Comments
 (0)