将至少包含 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++ 库来获取每个单词。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP