将具有至少 K 个字符的所有单词的第一个字符大写
给定表示一个句子的字符串str和一个整数K ,任务是将给定句子中至少有K个字符的所有单词大写。
例子:
Input: str = “geeks for geeks”, K = 4
Output: Geeks for Geeks
Explanation: The word “for” does not contain 4 characters, hence its 1st character is not capitalize.
Input: str = “geeksforgeeks is the best”, K = 0
Output: Geeksforgeeks Is The Best
方法:这是一个基于实现的问题。
- 这个想法是计算每个单词的字符数,并且,
- 如果字符数大于K ,
- 将单词的第一个字符的大小写更改为大写
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to capitalize the 1st
// character of all words having
// at least K characters
string capitalizeStr(string str, int K)
{
// Stores location of
// the 1st character of
// the current word
int ptr = 0;
// Loop to traverse string
for (int i = 0; i < str.size(); i++) {
// If the current word
// ends at index i
if (str[i] == ' ') {
// Update ptr
ptr = i + 1;
}
// Count of characters
// is at least K
else if (i - ptr + 1 >= K) {
str[ptr] = toupper(str[ptr]);
}
}
// Return answer
return str;
}
// Driver Code
int main()
{
string str = "geeksforgeeks is the best";
int K = 0;
cout << capitalizeStr(str, K);
return 0;
}
Java
// Java program of the above approach
import java.util.*;
class GFG
{
// Function to capitalize the 1st
// character of all words having
// at least K characters
public static String capitalizeStr(String str, int K)
{
// Stores location of
// the 1st character of
// the current word
int ptr = 0;
char[] ch = str.toCharArray();
// Loop to traverse string
for (int i = 0; i < ch.length; i++) {
// If the current word
// ends at index i
if (ch[i] == ' ') {
// Update ptr
ptr = i + 1;
}
// Count of characters
// is at least K
else if (i - ptr + 1 >= K) {
ch[ptr] = Character.toUpperCase(ch[ptr]);
}
}
String s = new String(ch);
// Return answer
return s;
}
// Driver Code
public static void main(String args[])
{
String str = "geeksforgeeks is the best";
int K = 0;
System.out.println(capitalizeStr(str, K));
}
}
// This code is contributed by Taranpreet
Python3
# python3 program of the above approach
# Function to capitalize the 1st
# character of all words having
# at least K characters
def capitalizeStr(str, K):
str = list(str)
# Stores location of
# the 1st character of
# the current word
ptr = 0
# Loop to traverse string
for i in range(0, len(str)):
# If the current word
# ends at index i
if (str[i] == ' '):
# Update ptr
ptr = i + 1
# Count of characters
# is at least K
elif (i - ptr + 1 >= K):
str[ptr] = str[ptr].upper()
# Return answer
return "".join(str)
# Driver Code
if __name__ == "__main__":
str = "geeksforgeeks is the best"
K = 0
print(capitalizeStr(str, K))
# This code is contributed by rakeshsahni
C#
// C# program of the above approach
using System;
class GFG {
// Function to capitalize the 1st
// character of all words having
// at least K characters
static string capitalizeStr(string str, int K)
{
// Stores location of
// the 1st character of
// the current word
int ptr = 0;
char[] ch = str.ToCharArray();
// Loop to traverse string
for (int i = 0; i < ch.Length; i++) {
// If the current word
// ends at index i
if (ch[i] == ' ') {
// Update ptr
ptr = i + 1;
}
// Count of characters
// is at least K
else if (i - ptr + 1 >= K) {
ch[ptr] = Char.ToUpper(ch[ptr]);
}
}
string s = string.Concat(ch);
// Return answer
return s;
}
// Driver Code
public static void Main()
{
string str = "geeksforgeeks is the best";
int K = 0;
Console.WriteLine(capitalizeStr(str, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Geeksforgeeks Is The Best
时间复杂度: O(N),其中 N 是字符串str 中的字符数。
辅助空间: O(1)