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

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.

// 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); 


Examples:

Input  :  str = "This is demo string" 
          char = i,    
          count = 3
Output :  ng

Input :  str = "geeksforgeeks"
         char = e, 
         count = 2
Output : ksforgeeks

No comments:

Post a Comment