Monday, November 18, 2024
Google search engine
HomeData Modelling & AICapitalize 1st character of all words having at least K characters

Capitalize 1st character of all words having at least K characters

Given string str representing a sentence, and an integer K, the task is to capitalize all the words in the given sentence having at least K characters.

Example:

Input: str = “neveropen for neveropen”, K = 4
Output: Geeks for Geeks
Explanation: The word “for” does not contain 4 characters, hence its 1st character is not capitalize.

Input: str = “neveropen is the best”, K = 0
Output: Geeksforneveropen Is The Best

 

Approach: This is an implementation-based problem. 
 

  • The idea is to calculate the character count of each word and,
  • if the count of characters is greater than K,
  • change the case of the 1st character of the word to the upper case

Below is the implementation of the above approach:

C++




// C++ program of the above approach
#include <bits/stdc++.h>
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 = "neveropen 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 = "neveropen 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 = "neveropen 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 = "neveropen is the best";
    int K = 0;
 
    Console.WriteLine(capitalizeStr(str, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript program of the above approach
 
// Function to capitalize the 1st
// character of all words having
// at least K characters
function capitalizeStr(str, K)
{
    str = str.split("")
     
    // Stores location of
    // the 1st character of
    // the current word
    let ptr = 0;
 
    // Loop to traverse string
    for (let i = 0; i < str.length; 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] = str[ptr].toUpperCase()
        }
    }
 
    // Return answer
    return str.join("");
}
 
// Driver Code
let str = "neveropen is the best";
let K = 0;
 
document.write(capitalizeStr(str, K))
 
// This code is contributed by saurabh_jaiswal.
</script>


Output

Geeksforneveropen Is The Best

Time Complexity: O(N), where N is the count of characters in string str.
Auxiliary space: O(1)

Last Updated :
16 Feb, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Take a part in the ongoing discussion

RELATED ARTICLES

Most Popular

Recent Comments