Problem Statement: Given a string, write a program to Capitalize the first and last character of each word of that string.
Examples:
Example 1: Input: String str = "take u forward is awesome" Output: “TakE U ForwarD IS AwesomE” Explanation: We get the result after capitalizing the first and last character of each word of a string Example 2: Input: String str = "Take u Forward is Awesome" Output: “TakE U ForwarD IS AwesomE” Explanation: Characters T, F, A are initially in uppercase , so they remain as they are in the result.
Solution
Disclaimer: Don’t jump directly to the solution, try it out yourself first.
Solution 1:
Approach:
- Keep on iterating the given string:
- Convert first and last index character to uppercase if initially the string character is in lowercase
- Convert the characters present before and after “space” to uppercase if initially the string character is in lowercase
- Return the final string
Things to note:
- (int) str[i] gives the ASCII value of the ith string character
- ((int) str[i] – 32) gives the ASCII value of the Capitalized ith string character
- (char)((int) str[i] – 32) converts the ASCII value to its corresponding character
- In Java , the StringBuffer class is used to create mutable (modifiable) strings.
Code:
C++ Code
#include<bits/stdc++.h>
using namespace std;
void Capitalize(string str, int size) {
for (int i = 0; i < size; i++) {
if (i == 0 || i == (size - 1) && (int) str[i] >= 97) // Converting first and
last index character to uppercase
{
str[i] = ((char)((int) str[i] - 32));
} else if (str[i] == ' ') // Converting characters present before and after
space to uppercase
{
if (((int) str[i - 1] - 32) >= 65) // Checking if already not an uppercase
letter
str[i - 1] = ((char)((int) str[i - 1] - 32));
if (((int) str[i + 1] - 32) >= 65) // Checking if already not an uppercase
letter
str[i + 1] = ((char)((int) str[i + 1] - 32));
}
}
cout<<"String after capitalizing the first and last letter of each word of the
string: "<< "\n";
cout << str << "\n";
}
int main() {
// Input string
string str = "take u forward is awesome";
int size = str.length();
Capitalize(str, size);
return 0;
}
Output:
String after capitalizing the first and last letter of each word of the string:
TakE U ForwarD IS AwesomE
Time Complexity: O(n), n is the length of string
Space Complexity: O(1)
Java Code
public class TUF {
public static String Capitalize(String str, int size) {
StringBuffer sb = new StringBuffer(str);
for (int i = 0; i < size; i++) {
if (i == 0 || i == (size - 1) && (int) str.charAt(i) >= 97) //Converting first
and last index character to uppercase
{
sb.setCharAt(i, (char)((int) str.charAt(i) - 32));
} else if (str.charAt(i) == ' ') // Converting characters present before and
after space to uppercase
{
if (((int) str.charAt(i - 1) - 32) >= 65) // Already not an uppercase letter
sb.setCharAt(i - 1, (char)((int) str.charAt(i - 1) - 32));
if (((int) str.charAt(i + 1) - 32) >= 65) // Already not an uppercase letter
sb.setCharAt(i + 1, (char)((int) str.charAt(i + 1) - 32));
}
}
return sb.toString();
}
public static void main(String args[]) {
String str = "take u forward is awesome";
int size = str.length();
System.out.println("String after capitalizing the first and last letter of each
word of the string: ");
System.out.println(Capitalize(str, size));
}
}
Output:
String after capitalizing the first and last letter of each word of the string:
TakE U ForwarD IS AwesomE
Time Complexity: O(n), n is the length of string
Space Complexity: O(n), because of StringBuffer
Solution 2: Using towupper() in C++ , Character.toUpperCase() in Java
Approach:
Similar approach as solution 1, the only change is, here we use the towupper function in C++ to capitalize the corresponding characters instead of using the ASCII values and typecasting approach.
towupper(): converts wide characters into uppercase
Character.toUpperCase(): converts the character argument to uppercase
Code:
C++ Code
#include<bits/stdc++.h>
using namespace std;
void Capitalize(string str, int size) {
for (int i = 0; i < size; i++) {
if (i == 0 || i == (size - 1)) // Converting first and last index character to
uppercase
{
str[i] = towupper(str[i]);
} else if (str[i] == ' ') // Converting characters present before and after
space to uppercase
{
str[i - 1] = towupper(str[i - 1]);
str[i + 1] = towupper(str[i + 1]);
}
}
cout<<"String after capitalizing the first and last letter of each word of the
string:" << "\n";
cout << str << "\n";
}
int main() {
// Input string
string str = "take u forward Is awesome";
int size = str.length();
Capitalize(str, size);
return 0;
}
Output:
String after capitalizing the first and last letter of each word of the string:
TakE U ForwarD IS AwesomE
Time Complexity: O(n), n is the length of string
Space Complexity: O(1)
Java Code
public class TUF {
public static String Capitalize(String str, int size) {
StringBuffer sb = new StringBuffer(str);
for (int i = 0; i < size; i++) {
if (i == 0 || i == (size - 1)) // Converting first and last index character to
uppercase
{
sb.setCharAt(i, Character.toUpperCase((char)(int) str.charAt(i)));
} else if (str.charAt(i) == ' ') // Converting characters present before and
after space to uppercase
{
sb.setCharAt(i - 1, Character.toUpperCase((char)(int) str.charAt(i - 1)));
sb.setCharAt(i + 1, Character.toUpperCase((char)(int) str.charAt(i + 1)));
}
}
return sb.toString();
}
public static void main(String args[]) {
String str = "Take u Forward is Awesome";
int size = str.length();
System.out.println("String after capitalizing first and last letter of each word
of the string: ");
System.out.println(Capitalize(str, size));
}
}
Output:
String after capitalizing the first and last letter of each word of the string:
TakE U ForwarD IS AwesomE
Time Complexity: O(n), n is the length of string
Space Complexity: O(n), because of StringBuffer
Special thanks to Rishiraj Girmal for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article