Print the string after the specified character has occurred given no. of times

16 Jun, 2022
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.Examples:  
Input  :  str = "This is demo string" 
          char = i,    
          count = 3
Output :  ng

Input :  str = "geeksforgeeks"
         char = e, 
         count = 2
Output : ksforgeeks
Asked in: Oracle 
Implementation: 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 the last, then print “Empty string”. 
C++
Java
Python3
C#
Javascript
// C++ program for above implementation#include <iostream>using namespace std; // Function to print the stringvoid printString(string str, char ch, int count){    int occ = 0, i;     // If given count is 0    // print the given string and return    if (count == 0) {        cout << str;        return;    }     // Start traversing the string    for (i = 0; i < str.length(); i++) {         // Increment occ if current char is equal        // to given character        if (str[i] == ch)            occ++;         // Break the loop if given character has        // been occurred given no. of times        if (occ == count)            break;    }     // Print the string after the occurrence    // of given character given no. of times    if (i < str.length() - 1)        cout << str.substr(i + 1, str.length() - (i + 1));     // Otherwise string is empty    else        cout << "Empty string";} // Drivers codeint main(){    string str = "geeks for geeks";    printString(str, 'e', 2);    return 0;}
// Java program for above implementation public class GFG{    // Method to print the string    static void printString(String str, char ch, int count)    {        int occ = 0, i;              // If given count is 0        // print the given string and return        if (count == 0) {            System.out.println(str);            return;        }              // Start traversing the string        for (i = 0; i < str.length(); i++) {                  // Increment occ if current char is equal            // to given character            if (str.charAt(i) == ch)                occ++;                  // Break the loop if given character has            // been occurred given no. of times            if (occ == count)                break;        }              // Print the string after the occurrence        // of given character given no. of times        if (i < str.length() - 1)            System.out.println(str.substring(i + 1));              // Otherwise string is empty        else            System.out.println("Empty string");    }         // Driver Method    public static void main(String[] args)    {        String str = "geeks for geeks";        printString(str, 'e', 2);    }}
# Python3 program for above implementation # Function to print the stringdef printString(str, ch, count):    occ, i = 0, 0     # If given count is 0    # print the given string and return    if (count == 0):        print(str)     # 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):            break     # 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 codeif __name__ == '__main__':    str = "geeks for geeks"    printString(str, 'e', 2) # This code is contributed# by 29AjayKumar
// C# program for above implementationusing System; public class GFG {         // Method to print the string    static public void printString(string str,                           char ch, int count)    {        int occ = 0, i;             // If given count is 0        // print the given string        // and return        if (count == 0) {            Console.WriteLine(str);            return;        }             // Start traversing the string        for (i = 0; i < str.Length; i++)        {                 // Increment occ if current            // char is equal to given            // character            if (str[i] == ch)                occ++;                 // Break the loop if given            // character has been occurred            // given no. of times            if (occ == count)                break;        }             // Print the string after the        // occurrence of given character        // given no. of times        if (i < str.Length - 1)            Console.WriteLine(str.Substring(i + 1));             // Otherwise string is empty        else            Console.WriteLine("Empty string");    }         // Driver Method    static public void Main()    {        string str = "geeks for geeks";        printString(str, 'e', 2);    }} // This code is contributed by vt_m.
<script> // JavaScript program for above implementation // Method to print the stringfunction printString(str, ch , count){    var occ = 0, i;      // If given count is 0    // print the given string and return    if (count == 0) {        document.write(str);        return;    }      // Start traversing the string    for (i = 0; i < str.length; i++) {          // Increment occ if current char is equal        // to given character        if (str.charAt(i) == ch)            occ++;          // Break the loop if given character has        // been occurred given no. of times        if (occ == count)            break;    }      // Print the string after the occurrence    // of given character given no. of times    if (i < str.length - 1)        document.write(str.substring(i + 1));      // Otherwise string is empty    else        document.write("Empty string");} // Driver Methodvar str = "geeks for geeks";printString(str, 'e', 2); // This code is contributed by Amit Katiyar </script>
Output:  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
ks for geeks
Time complexity : O(n) Auxiliary Space : O(1) 
Print the string after the specified character has occurred given no. of times | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersPrint the string after the specified character has occurred given no. of times | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:44•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=v3vPtLKr9s4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
?list=PLqM7alHXFySEmN22LjriVE8wTkkZA3tDB
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
vt_m
29AjayKumar
amit143katiyar
youmailmahibagi
Oracle
Zoho
Strings
Zoho
Oracle
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.