Given a string, a character, and a count, the task is to print the string after the specified character has occurred count number of times.Print “Empty string” in case of any unsatisfying conditions.(Given character is not present, or present but less than given count, or given count completes on last index). If given count is 0, then given character doesn’t matter, just print the whole string.
Input : str = "This is demo string" char = i, count = 3 Output : ng Input : str = "geeksforgeeks" char = e, count = 2 Output : ksforgeeks
Asked in: Oracle
1- Start traversing the string.
- Increment occ_count if current char is equal to given char.
- Get out of the loop, if occ_count becomes equal to given count.
2- Print the string after the index till the string gets traversed in the loop.
3- If index has reached to the last, then print “Empty string”.
# Python3 program for above implementation
# Function to print the string
def printString(str, ch, count):
occ, i = 0, 0
# If given count is 0
# print the given string and return
if (count == 0):
# Start traversing the string
for i in range(len(str)):
# Increment occ if current char
# is equal to given character
if (str[i] == ch):
occ += 1
# Break the loop if given character has
# been occurred given no. of times
if (occ == count):
# Print the string after the occurrence
# of given character given no. of times
if (i < len(str)- 1): print(str[i + 1: len(str) - i + 2]) # Otherwise string is empty else: print("Empty string") # Driver code if __name__ == '__main__': str = "geeks for geeks" printString(str, 'e', 2) # This code is contributed # by 29AjayKumar [tabby title="C#"]
ks for geeks
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Replace every character of string by character whose ASCII value is K times more than it
- Encrypt a string by repeating i-th character i times
- Decrypt a string encrypted by repeating i-th character i times
- Check if max occurring character of one string appears same no. of times in other
- Print last character of each word in a string
- Print the first and last character of each word in a String
- Print the string by ignoring alternate occurrences of any character
- Print Kth character in sorted concatenated substrings of a string
- Number of sub-strings that contain the given character exactly k times
- Count of substrings which contains a given character K times
- Count substrings with each character occurring at most k times
- Longest subsequence where every character appears at-least k times
- Longest subsequence where each character occurs at least k times
- Lexicographically largest subsequence such that every character occurs at least k times
- Find a string such that every character is lexicographically greater than its immediate next character