將至少包含 K 個字元的所有單詞的首字母大寫


在英語中,寫句子時需要以大寫字母開頭,並且對於任何城市/人名等,都需要以大寫字母開頭。在本問題中,給定一個字串和一個數字,我們需要更新給定字串中所有單詞的第一個字元,前提是它們的長度不小於 k。此外,如果單詞的長度大於 k 且其第一個字元已經是大寫,則我們將保持不變。

示例

輸入

string str = “this is the given string To change” 
int k = 5

輸出

this is the Given String To Change

輸入

string str = “thisisthegivenstring” 
int k = 8

輸出

“Thisisthegivenstring”

樸素方法

在這種方法中,我們將遍歷字串並找到空格字元,因為在每個空格字元處,我們將找到一個新單詞。

首先,我們將建立一個指標指向單詞的第一個字元,並且將有另一個指標遍歷字串,直到檢測到空格或字串的末尾。

我們將使用 toupper() 函式將字串的第一個字元大寫。如果第一個字元已經是大寫,則它將保持不變,否則將更改。

此外,可能存在兩個連續空格的情況,為此,我們將檢查兩個指標是否位於相同的位置,如果是則無需更改任何內容,並移動到下一個字元。

示例

#include <bits/stdc++.h>
using namespace std; 

// creating a function to change the given string 
string changeString(string str, int k){
   int len = str.length(); // storing the size of the given string 
   int ptr = 0; // pointer to point at the first character 
   for(int i=0; i<len; i++){
      if(str[i] == ' '){
         // if the pointer and the current pointer are in the same position either case of double whitespace characters or whitespace at the start of the string 
         if(i != ptr && i-ptr >= k){
            str[ptr] = toupper(str[ptr]);
         }
         ptr = i+1;
      }
   }
   // condition for the last word 
   if(ptr != len){
      str[ptr] = toupper(str[ptr]);
   }
   return str;
}

int main(){
   string str = "this is the given string To change"; // given string 
   int k = 5; // given number 
   // calling the function 
   cout<<changeString(str, k)<<endl;
   return 0; 
}

輸出

this is the Given String To Change

時間和空間複雜度

上述程式碼的時間複雜度為 O(N),其中 N 是給定字串的長度。

上述程式碼的空間複雜度為 O(1),因為我們在這裡沒有使用任何額外的空間,並且更改了給定的字串。

使用 C++ 的 StringStream 類

StringStream 是在 C++ 程式語言中定義的一個類,用於將字串處理為字元流。我們可以使用字元“<<”以流的形式獲取單詞,每次獲取一個單詞並將其儲存在字串變數中。

在此程式中,我們使用了相同的概念並建立了一個 stringstream 變數來儲存給定的字串,然後建立了一個字串變數來從字串中獲取單詞,以及另一個變數來儲存答案。

我們使用了 while 迴圈,使用“<<”提取運算子在流上獲取單詞序列,並在需要時將每個單詞的第一個字元大寫。

此外,我們將每個單詞儲存在字串中,並且需要在每個單詞後新增額外的空格,並將返回該答案。

示例

#include <bits/stdc++.h>
using namespace std; 

// creating a function to change the given string 
string changeString(string str, int k){
   stringstream s(str); //creating the stringstream variable 
   string ans = ""; // string to store the answer 
   string cur_word; // string to get the words from the string stream 
   while( s >> cur_word){
      // if length of this word is less than k continue otherwise update the value of the first character if needed 
      if(cur_word.length() < k){
         ans += cur_word;
      }
      else{
         cur_word[0] = toupper(cur_word[0]);
         ans += cur_word;
      }
      // adding space after each word 
      ans += " ";
   }
   ans.pop_back(); // removing the last added space 
   return ans; //return the final string. 
}
int main(){
   string str = "this is the given string To change"; // given string 
   int k = 5; // given number 
   // calling the function 
   cout<<changeString(str, k)<<endl;
   return 0; 
}

輸出

this is the Given String To Change

時間和空間複雜度

上述程式碼的時間複雜度為 O(N),其中 N 是給定字串的長度。

上述程式碼的空間複雜度為 O(N),因為我們在這裡使用了一個額外的空間,即一個字串來儲存流和答案。

結論

在本教程中,我們實現了一個程式,如果給定字串的長度大於給定數字,則將該字串的每個單詞大寫。我們實現了兩種方法;一種是使用 toupper() C++ 函式將每個單詞的第一個字元大寫,並簡單地使用兩個指標遍歷字串。在第二種方法中,我們使用了 stringstreamc++ 庫來獲取每個單詞。

更新於: 2023年8月31日

63 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.