给定 no 出现指定字符后打印字符串。次
给定一个字符串、一个字符和一个计数,任务是在指定字符出现计数次数后打印字符串。如果有任何不满意的条件,打印“Empty 字符串”。 (给定字符不存在,或存在但小于给定计数,或给定计数在最后一个索引上完成)。如果给定计数为 0,则给定字符无关紧要,只需打印整个字符串。
例子:
Input : str = "This is demo string"
char = i,
count = 3
Output : ng
Input : str = "geeksforgeeks"
char = e,
count = 2
Output : ksforgeeks
问:甲骨文
执行:
1-开始遍历字符串。
- 如果当前字符等于给定字符,则增加 occ_count。
- 如果 occ_count 等于给定计数,则退出循环。
2-在索引之后打印字符串,直到字符串在循环中遍历。
3-如果索引已到达最后一个,则打印“空字符串”。
C++
// C++ program for above implementation
#include
using namespace std;
// Function to print the string
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) {
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 code
int main()
{
string str = "geeks for geeks";
printString(str, 'e', 2);
return 0;
}
Java
// 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
# 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):
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 code
if __name__ == '__main__':
str = "geeks for geeks"
printString(str, 'e', 2)
# This code is contributed
# by 29AjayKumar
C#
// C# program for above implementation
using 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.
Javascript
输出:
ks for geeks