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:
- 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.
- Print the string after the index till the string gets traversed in the loop.
- If index has reached the last, then print “Empty string”.
Implementation:
C++
#include <iostream>
using namespace std;
void printString(string str, char ch, int count)
{
int occ = 0, i;
if (count == 0) {
cout << str;
return ;
}
for (i = 0; i < str.length(); i++) {
if (str[i] == ch)
occ++;
if (occ == count)
break ;
}
if (i < str.length() - 1)
cout << str.substr(i + 1, str.length() - (i + 1));
else
cout << "Empty string" ;
}
int main()
{
string str = "geeks for geeks" ;
printString(str, 'e' , 2);
return 0;
}
|
Java
public class GFG
{
static void printString(String str, char ch, int count)
{
int occ = 0 , i;
if (count == 0 ) {
System.out.println(str);
return ;
}
for (i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == ch)
occ++;
if (occ == count)
break ;
}
if (i < str.length() - 1 )
System.out.println(str.substring(i + 1 ));
else
System.out.println( "Empty string" );
}
public static void main(String[] args)
{
String str = "geeks for geeks" ;
printString(str, 'e' , 2 );
}
}
|
Python3
def printString( str , ch, count):
occ, i = 0 , 0
if (count = = 0 ):
print ( str )
for i in range ( len ( str )):
if ( str [i] = = ch):
occ + = 1
if (occ = = count):
break
if (i < len ( str ) - 1 ):
print ( str [i + 1 : len ( str ) - i + 2 ])
else :
print ( "Empty string" )
if __name__ = = '__main__' :
str = "geeks for geeks"
printString( str , 'e' , 2 )
|
C#
using System;
public class GFG {
static public void printString( string str,
char ch, int count)
{
int occ = 0, i;
if (count == 0) {
Console.WriteLine(str);
return ;
}
for (i = 0; i < str.Length; i++)
{
if (str[i] == ch)
occ++;
if (occ == count)
break ;
}
if (i < str.Length - 1)
Console.WriteLine(str.Substring(i + 1));
else
Console.WriteLine( "Empty string" );
}
static public void Main()
{
string str = "geeks for geeks" ;
printString(str, 'e' , 2);
}
}
|
Javascript
<script>
function printString(str, ch , count)
{
var occ = 0, i;
if (count == 0) {
document.write(str);
return ;
}
for (i = 0; i < str.length; i++) {
if (str.charAt(i) == ch)
occ++;
if (occ == count)
break ;
}
if (i < str.length - 1)
document.write(str.substring(i + 1));
else
document.write( "Empty string" );
}
var str = "geeks for geeks" ;
printString(str, 'e' , 2);
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
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.