- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Capitalize 1st character of all words having at least K characters
In the English language while writing a sentence we need to start with the capital character and for any name of the city/person, etc we begin with the capital letter. Here in this problem, we are given a string and a number and we have to update the first character of all the words of the given string if their size is not less than k. Also, if the size of the words is more than k and their first character is already capitalized then we will leave it as it is.
Sample Examples
Input
string str = “this is the given string To change” int k = 5
Output
this is the Given String To Change
Input
string str = “thisisthegivenstring” int k = 8
Output
“Thisisthegivenstring”
Naive Approach
In this approach, we are going to traverse over the string and will find the whitespace characters because, at each whitespace character, we will find the new word.
First, we will create a pointer that will point to the first character of the word and there will be another pointer that will traverser over the string until a whitespace or the end of the string is not detected.
We will use the toupper() function to capitalize the first character of the string. If the first character is already capitalized then it will remain the same otherwise it will be changed.
Also, there may be situtation of the two continuous whitespaces, for that we will check if both the pointers are at the same poisition then there is no need to change anything and move to the next character.
Example
#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; }
Output
this is the Given String To Change
Time and Space Complexity
The time complexity of the above code is O(N), where N is the length of the given string.
The space complexity of the above code is O(1), as we are not using any extra space here and changed the given string.
Using StringStream Class of C++
StringStream is a class defined in the C++ programming language and used to process the string as the stream of the characters. We can use the character “<<” to get the words in the form of the stream where each time a single word will be come and stored in a string variable.
In this program, we have used the same concept and created a stringstream variable to store the given string and then created a string variable to get the words from the sting and another variable to store the answer.
We have used the while loop to get the sequence of the words using the “<<” extraction operator over the stream and we have capitalized the first character of each word if needed.
Also, we will store each word in the string and we need to add extra space after each word and will return that answer.
Example
#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; }
Output
this is the Given String To Change
Time and Space Complexity
The time complexity of the above code is O(N), where N is the length of the given string.
The space complexity of the above code is O(N), as we are using an extra space here that is a string to store the stream and the answer.
Conclusion
In this tutorial, we have implemented a program to capitalize each word of the given string if its length is greater than the given number. We have implemented two approaches; one is by using the toupper() C++ function to capitalize the first character of each word and simply traversing over the string by using two pointers. In the second approach, we have used the stringstreamc++ library to get each word.