Write a program to convert the first character uppercase in a sentence and if apart from the first character if any other character is in Uppercase then convert it into Lowercase.
Examples:
Input : gEEKs
Output : GeeksInput : GFG
Output : GfgInput : Lazyroar
Output : Geeksforgeeks
Method 1:
- Iterate through each character in the input string.
- If the current character is the first character of a word (or the first character of the sentence):
- If the character is a lowercase letter, convert it to uppercase using the ASCII value manipulation:
str[i] = (char)(str[i] - 'a' + 'A').
- If the character is a lowercase letter, convert it to uppercase using the ASCII value manipulation:
- If the current character is not the first character of a word:
- If the character is an uppercase letter, convert it to lowercase using the ASCII value manipulation:
str[i] = (char)(str[i] - 'a' + 'A').
- If the character is an uppercase letter, convert it to lowercase using the ASCII value manipulation:
Below is the implementation of above approach:
C++
// C++ program to convert// first character uppercase// in a sentence#include<bits/stdc++.h>using namespace std;string convert(string str){ for (int i = 0; i < str.length(); i++) { // If first character of a // word is found if (i == 0 && str[i] != ' ' || str[i] != ' ' && str[i - 1] == ' ') { // If it is in lower-case if (str[i] >= 'a' && str[i] <= 'z') { // Convert into Upper-case str[i] = (char)(str[i] - 'a' + 'A'); } } // If apart from first character // Any one is in Upper-case else if (str[i] >= 'A' && str[i] <= 'Z') // Convert into Lower-Case str[i] = (char)(str[i] + 'a' - 'A'); } return str;}// Driver codeint main(){ string str = "gEEks fOr GeeKs"; cout << (convert(str)); return 0;}// This code is contributed by Chitranayal |
Java
// Java program to convert first character// uppercase in a sentenceclass GFG { static String convert(String str) { // Create a char array of given String char ch[] = str.toCharArray(); for (int i = 0; i < str.length(); i++) { // If first character of a word is found if (i == 0 && ch[i] != ' ' || ch[i] != ' ' && ch[i - 1] == ' ') { // If it is in lower-case if (ch[i] >= 'a' && ch[i] <= 'z') { // Convert into Upper-case ch[i] = (char)(ch[i] - 'a' + 'A'); } } // If apart from first character // Any one is in Upper-case else if (ch[i] >= 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = (char)(ch[i] + 'a' - 'A'); } // Convert the char array to equivalent String String st = new String(ch); return st; } public static void main(String[] args) { String str = "gEEks fOr GeeKs"; System.out.println(convert(str)); }} |
Python3
# Python3 program to convert first character# uppercase in a sentencedef convert(s): # Create a char array of # given string ch = list(s) for i in range(len(s)): # If first character of a word is found if (i == 0 and ch[i] != ' ' or ch[i] != ' ' and ch[i - 1] == ' '): # If it is in lower-case if (ch[i] >= 'a' and ch[i] <= 'z'): # Convert into Upper-case ch[i] = chr(ord(ch[i]) - ord('a') + ord('A')) # If apart from first character # Any one is in Upper-case elif (ch[i] >= 'A' and ch[i] <= 'Z'): # Convert into Lower-Case ch[i] = chr(ord(ch[i]) + ord('a') - ord('A')) # Convert the char array # to equivalent string st = "".join(ch) return st;# Driver code if __name__=="__main__": s = "gEEks fOr GeeKs" print(convert(s)); # This code is contributed by rutvik_56 |
C#
// C# program to convert first character// uppercase in a sentenceusing System;class GFG { static String convert(String str) { // Create a char array of // given String char []ch = str.ToCharArray(); for (int i = 0; i < str.Length; i++) { // If first character of a // word is found if (i == 0 && ch[i] != ' ' || ch[i] != ' ' && ch[i - 1] == ' ') { // If it is in lower-case if (ch[i] >= 'a' && ch[i] <= 'z') { // Convert into Upper-case ch[i] = (char)(ch[i] - 'a' + 'A'); } } // If apart from first character // Any one is in Upper-case else if (ch[i] >= 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = (char)(ch[i] + 'a' - 'A'); } // Convert the char array to // equivalent String String st = new String(ch); return st; } // Driver code public static void Main() { String str = "gEEks fOr GeeKs"; Console.Write(convert(str)); }}// This code is contributed by Nitin Mittal. |
Javascript
<script>// Javascript program to convert first character// uppercase in a sentence function convert(str) { // Create a char array of given String let ch = str.split(""); for (let i = 0; i < str.length; i++) { // If first character of a word is found if (i == 0 && ch[i] != ' ' || ch[i] != ' ' && ch[i - 1] == ' ') { // If it is in lower-case if (ch[i] >= 'a' && ch[i] <= 'z') { // Convert into Upper-case ch[i] = String.fromCharCode(ch[i].charCodeAt(0) - 'a'.charCodeAt(0) + 'A'.charCodeAt(0)); } } // If apart from first character // Any one is in Upper-case else if (ch[i] >= 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = String.fromCharCode(ch[i].charCodeAt(0) + 'a'.charCodeAt(0) - 'A'.charCodeAt(0)); } // Convert the char array to equivalent String let st = (ch).join(""); return st; } let str = "gEEks fOr GeeKs"; document.write(convert(str)); // This code is contributed by avanitrachhadiya2155</script> |
Geeks For Geeks
Time Complexity: O(n), where n is the length of the given sentence.
Auxiliary Space: O(1)
Program to convert first character uppercase in a sentence using Inbuilt methods:
Use inbuilt methods of different programming languages to convert a character to lowercase or uppercase accordingly.
Below is the implementation:
C++
#include <bits/stdc++.h>using namespace std;// Method to convert the stringstring capitailizeWord(string str){ stringstream s(str); string word; string result = ""; // Declare a character of space // To identify that the next character is the starting // of a new word while (s >> word) { word[0] = toupper(word[0]); result += word + " "; } // Return the string with trimming return result.substr(0, result.length() - 1);}// Driver Codeint main(){ // Declare the string string s1 = "gEEks fOr GeeKs"; // Convert that string into lowercase transform(s1.begin(), s1.end(), s1.begin(), ::tolower); // Call the method to capitalize each word cout << capitailizeWord(s1);} |
Java
// Java Program to capitalize each word in a stringpublic class GFG { // Method to convert the string static String capitailizeWord(String str) { StringBuffer s = new StringBuffer(); // Declare a character of space // To identify that the next character is the // starting of a new word char ch = ' '; for (int i = 0; i < str.length(); i++) { // If previous character is space and current // character is not space then it shows that // current letter is the starting of the word if (ch == ' ' && str.charAt(i) != ' ') s.append( Character.toUpperCase(str.charAt(i))); else s.append(str.charAt(i)); ch = str.charAt(i); } // Return the string with trimming return s.toString().trim(); } // Driver Code public static void main(String args[]) { // Declare the string String s1 = "gEEks fOr GeeKs"; // Convert that string into lowercase s1 = s1.toLowerCase(); // Call the method to capitalize each word System.out.println(capitailizeWord(s1)); }} |
Python3
import stringimport io# Method to convert the stringdef capitalize_word(s): # Create a stream from the string s = io.StringIO(s) result = "" # Declare a character of space # To identify that the next character is the starting # of a new word for word in s.read().split(): word = word.lower() word = word.capitalize() result += word + " " # Return the string with trimming return result.strip()# Driver Codeif __name__ == "__main__": # Declare the string s1 = "gEEks fOr GeeKs" # Call the method to capitalize each word print(capitalize_word(s1)) |
C#
using System;public class GFG { // Method to convert the string static string CapitalizeWord(string str) { // Declare a character of space // To identify that the next character is the // starting of a new word char ch = ' '; string result = ""; for (int i = 0; i < str.Length; i++) { // If previous character is space and current // character is not space then it shows that // current letter is the starting of the word if (ch == ' ' && str[i] != ' ') result += char.ToUpper(str[i]); else result += str[i]; ch = str[i]; } // Return the string with trimming return result.Trim(); } // Driver Code public static void Main(string[] args) { // Declare the string string s1 = "gEEks fOr GeeKs"; // Convert that string into lowercase s1 = s1.ToLower(); // Call the method to capitalize each word Console.WriteLine(CapitalizeWord( s1)); // expected output: "Geeks For Geeks" }} |
Javascript
function capitalizeWord(str) { let result = ''; let ch = ' '; for (let i = 0; i < str.length; i++) { if (ch === ' ' && str.charAt(i) !== ' ') { result += str.charAt(i).toUpperCase(); } else { result += str.charAt(i); } ch = str.charAt(i); } return result.trim();}let s1 = 'gEEks fOr GeeKs';s1 = s1.toLowerCase();console.log(capitalizeWord(s1)); // Output: "Geeks For Geeks" |
Geeks For Geeks
Time complexity: O(n)
Auxiliary Space: O(n)
