Strings Practice
String manipulation, slicing, formatting and text processing problems.
1
Reverse a String
Easy
Write a program to reverse the given string and print it.
Input Format
A single line containing a string S.
Output Format
Print the reversed string.
Sample Test Cases
|
Sample Input 1
hello |
Sample Output 1
olleh |
|
Sample Input 2
racecar |
Sample Output 2
racecar |
|
Sample Input 3
Python |
Sample Output 3
nohtyP |
2
Count Vowels in a String
Easy
Write a program to count the number of vowels (a, e, i, o, u) in a given string. Case-insensitive.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of vowels.
Sample Test Cases
|
Sample Input 1
education |
Sample Output 1
5 |
|
Sample Input 2
Hello |
Sample Output 2
2 |
|
Sample Input 3
Python |
Sample Output 3
1 |
3
Check Palindrome String
Easy
Write a program to check whether the given string reads the same forwards and backwards.
Input Format
A single line containing a string S.
Output Format
Print YES if palindrome, else NO.
Sample Test Cases
|
Sample Input 1
madam |
Sample Output 1
YES |
|
Sample Input 2
racecar |
Sample Output 2
YES |
|
Sample Input 3
hello |
Sample Output 3
NO |
4
Convert to Uppercase
Easy
Write a program to convert the entire given string to uppercase.
Input Format
A single line containing a string S.
Output Format
Print the string in uppercase.
Sample Test Cases
|
Sample Input 1
hello world |
Sample Output 1
HELLO WORLD |
|
Sample Input 2
Tech Bridge |
Sample Output 2
TECH BRIDGE |
|
Sample Input 3
abc123 |
Sample Output 3
ABC123 |
5
Length of String
Easy
Write a program to find and print the length of the given string.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the length of S.
Sample Test Cases
|
Sample Input 1
TechBridge24 |
Sample Output 1
12 |
|
Sample Input 2
Hi |
Sample Output 2
2 |
|
Sample Input 3
Hello World |
Sample Output 3
11 |
6
Remove Spaces
Easy
Write a program to remove all whitespace characters from the given string.
Input Format
A single line containing a string S with spaces.
Output Format
Print the string without any spaces.
Sample Test Cases
|
Sample Input 1
hello world python |
Sample Output 1
helloworldpython |
|
Sample Input 2
a b c |
Sample Output 2
abc |
|
Sample Input 3
python |
Sample Output 3
python |
7
First and Last Character
Easy
Write a program to print the first and last character of the given string on separate lines.
Input Format
A single line containing a string S.
Output Format
Print the first character on line 1 and the last character on line 2.
Sample Test Cases
|
Sample Input 1
python |
Sample Output 1
p n |
|
Sample Input 2
hello |
Sample Output 2
h o |
|
Sample Input 3
ab |
Sample Output 3
a b |
8
Count Words in a String
Easy
Write a program to count the number of words in the given string. Words are separated by single spaces.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of words.
Sample Test Cases
|
Sample Input 1
I love python programming |
Sample Output 1
4 |
|
Sample Input 2
one two three |
Sample Output 2
3 |
|
Sample Input 3
python |
Sample Output 3
1 |
9
Check if String Contains Digit
Easy
Write a program to check whether the given string contains at least one digit.
Input Format
A single line containing a string S.
Output Format
Print YES if it contains a digit, else NO.
Sample Test Cases
|
Sample Input 1
python3 |
Sample Output 1
YES |
|
Sample Input 2
hello |
Sample Output 2
NO |
|
Sample Input 3
123 |
Sample Output 3
YES |
10
Swap Case of Characters
Easy
Write a program to swap the case of each character (lower to upper and vice versa) in the given string.
Input Format
A single line containing a string S.
Output Format
Print the string with swapped case.
Sample Test Cases
|
Sample Input 1
HelloWorld |
Sample Output 1
hELLOwORLD |
|
Sample Input 2
PYTHON |
Sample Output 2
python |
|
Sample Input 3
aBc |
Sample Output 3
AbC |
1
Count Each Character Frequency
Medium
Write a program to count the frequency of each character in the given string.
Input Format
A single line containing a string S.
Output Format
Print each character and its count as char: count, one per line.
Sample Test Cases
|
Sample Input 1
abracadabra |
Sample Output 1
a: 5 b: 2 c: 1 d: 1 r: 2 |
|
Sample Input 2
hello |
Sample Output 2
h: 1 e: 1 l: 2 o: 1 |
|
Sample Input 3
aabb |
Sample Output 3
a: 2 b: 2 |
2
Remove Duplicate Characters
Medium
Write a program to remove duplicate characters from the string while preserving the order of first occurrence.
Input Format
A single line containing a string S.
Output Format
Print the string after removing duplicates.
Sample Test Cases
|
Sample Input 1
programming |
Sample Output 1
progamin |
|
Sample Input 2
banana |
Sample Output 2
ban |
|
Sample Input 3
hello |
Sample Output 3
helo |
3
Check Anagram Strings
Medium
Write a program to check whether two strings are anagrams of each other.
Input Format
First line: string A. Second line: string B.
Output Format
Print YES if anagrams, else NO.
Sample Test Cases
|
Sample Input 1
listen |
Sample Output 1
silent |
|
Sample Input 2
listen silent |
Sample Output 2
YES |
|
Sample Input 3
hello world |
Sample Output 3
NO |
Constraints
YES
4
Reverse Words in a String
Medium
Write a program to reverse the order of words in the given sentence.
Input Format
A single line containing a string S with words separated by single spaces.
Output Format
Print the sentence with words reversed.
Sample Test Cases
|
Sample Input 1
I love python |
Sample Output 1
python love I |
|
Sample Input 2
hello world |
Sample Output 2
world hello |
|
Sample Input 3
a b c |
Sample Output 3
c b a |
5
Longest Word in a String
Medium
Write a program to find and print the longest word in the given sentence. If there are multiple, print the first one.
Input Format
A single line containing a string S.
Output Format
Print the longest word.
Sample Test Cases
|
Sample Input 1
python is a powerful language |
Sample Output 1
powerful |
|
Sample Input 2
short and verylong |
Sample Output 2
verylong |
|
Sample Input 3
a bb ccc |
Sample Output 3
ccc |
6
Check Substring Present
Medium
Write a program to check whether a substring is present inside the main string.
Input Format
First line: string S. Second line: substring T.
Output Format
Print YES if T is present in S, else NO.
Sample Test Cases
|
Sample Input 1
Hello World |
Sample Output 1
World |
|
Sample Input 2
Hello World lo Wo |
Sample Output 2
YES |
|
Sample Input 3
abc xyz |
Sample Output 3
NO |
Constraints
YES
7
Count Consonants
Medium
Write a program to count the number of consonants in the given string.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of consonants.
Sample Test Cases
|
Sample Input 1
programming |
Sample Output 1
9 |
|
Sample Input 2
hello |
Sample Output 2
3 |
|
Sample Input 3
aeiou |
Sample Output 3
0 |
8
Replace All Spaces with Hyphen
Medium
Write a program to replace every space in the given string with a hyphen (-).
Input Format
A single line containing a string S.
Output Format
Print the modified string.
Sample Test Cases
|
Sample Input 1
hello world python |
Sample Output 1
hello-world-python |
|
Sample Input 2
a b |
Sample Output 2
a-b |
|
Sample Input 3
x y |
Sample Output 3
x--y |
9
Capitalize First Letter of Each Word
Medium
Write a program to capitalize the first letter of every word in the given string.
Input Format
A single line containing a string S.
Output Format
Print the string with each word capitalized.
Sample Test Cases
|
Sample Input 1
python is great |
Sample Output 1
Python Is Great |
|
Sample Input 2
hello world |
Sample Output 2
Hello World |
|
Sample Input 3
a b |
Sample Output 3
A B |
10
Check if String is Pangram
Medium
Write a program to check whether the given string is a pangram (contains every letter a–z at least once).
Input Format
A single line containing a string S.
Output Format
Print YES if pangram, else NO.
Sample Test Cases
|
Sample Input 1
The quick brown fox jumps over the lazy dog |
Sample Output 1
YES |
|
Sample Input 2
hello |
Sample Output 2
NO |
|
Sample Input 3
abcdefghijklmnopqrstuvwxyz |
Sample Output 3
YES |
1
Longest Substring Without Repeating Characters
Hard
Write a program to find the length of the longest substring without repeating characters.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the length of the longest substring.
Sample Test Cases
|
Sample Input 1
abcabcbb |
Sample Output 1
3 |
|
Sample Input 2
bbbbb |
Sample Output 2
1 |
|
Sample Input 3
pwwkew |
Sample Output 3
3 |
2
Minimum Window Substring
Hard
Write a program to find the smallest substring in S that contains all characters of string T.
Input Format
First line: string S. Second line: string T.
Output Format
Print the smallest window substring; print -1 if no such window exists.
Sample Test Cases
|
Sample Input 1
ADOBECODEBANC |
Sample Output 1
ABC |
|
Sample Input 2
a aa |
Sample Output 2
NO |
|
Sample Input 3
abc b |
Sample Output 3
b |
Constraints
BANC
3
String Rotation Check
Hard
Write a program to check whether string B is a rotation of string A.
Input Format
First line: string A. Second line: string B.
Output Format
Print YES if B is a rotation of A, else NO.
Sample Test Cases
|
Sample Input 1
ABCD |
Sample Output 1
CDAB |
|
Sample Input 2
ABCD BCDA |
Sample Output 2
YES |
|
Sample Input 3
ABCD ABCD |
Sample Output 3
YES |
Constraints
YES
4
Longest Palindromic Substring
Hard
Write a program to find the longest palindromic substring in the given string.
Input Format
A single line containing a string S.
Output Format
Print the longest palindromic substring.
Sample Test Cases
|
Sample Input 1
babad |
Sample Output 1
bab |
|
Sample Input 2
cbbd |
Sample Output 2
bb |
|
Sample Input 3
a |
Sample Output 3
a |
5
String to Integer (atoi)
Hard
Write a program to convert the given numeric string to an integer without using int(). Handle optional leading + or - sign.
Input Format
A single line containing a numeric string S.
Output Format
Print the integer value.
Sample Test Cases
|
Sample Input 1
-12345 |
Sample Output 1
-12345 |
|
Sample Input 2
+123 |
Sample Output 2
123 |
|
Sample Input 3
007 |
Sample Output 3
7 |
6
Print All Substrings of a String
Hard
Write a program to print all substrings of the given string, one per line, in order of increasing length.
Input Format
A single line containing a string S.
Output Format
Print all substrings, one per line.
Sample Test Cases
|
Sample Input 1
abc |
Sample Output 1
a b c ab bc abc |
|
Sample Input 2
ab |
Sample Output 2
a b ab |
|
Sample Input 3
xyz |
Sample Output 3
x y z xy yz xyz |
7
Group Anagrams Together
Hard
Write a program to group words that are anagrams of each other from a list of words.
Input Format
First line: integer N. Second line: N space-separated words.
Output Format
Print each group as a space-separated line, groups in order of first appearance.
Sample Test Cases
|
Sample Input 1
5 eat tea tan ate nat |
Sample Output 1
eat tea ate tan nat |
|
Sample Input 2
4 dog god act |
Sample Output 2
dog god act |
|
Sample Input 3
3 cat act tac |
Sample Output 3
cat act tac |
8
First Non-Repeating Character
Hard
Write a program to find the first non-repeating character in the given string.
Input Format
A single line containing a string S.
Output Format
Print the first non-repeating character, or -1 if none exists.
Sample Test Cases
|
Sample Input 1
swiss |
Sample Output 1
w |
|
Sample Input 2
aabbcc |
Sample Output 2
-1 |
|
Sample Input 3
xxyz |
Sample Output 3
y |
9
Compress the String (Run-Length)
Hard
Write a program to compress the string using run-length encoding: char followed by its count.
Input Format
A single line containing a string S.
Output Format
Print the compressed string.
Sample Test Cases
|
Sample Input 1
aaabbcccc |
Sample Output 1
a3b2c4 |
|
Sample Input 2
aab |
Sample Output 2
a2b1 |
|
Sample Input 3
aaa |
Sample Output 3
a3 |
10
Zigzag String Conversion
Hard
Write a program to convert the given string into a zigzag pattern with numRows rows and read it row by row.
Input Format
First line: string S. Second line: integer numRows.
Output Format
Print the zigzag-converted string.
Sample Test Cases
|
Sample Input 1
PAYPALISHIRING |
Sample Output 1
3 |
|
Sample Input 2
AB 1 |
Sample Output 2
AB |
|
Sample Input 3
ABCD 2 |
Sample Output 3
ACBD |
Constraints
PAHNAPLSIIGYIR