Computer Science, asked by ajeek9860, 6 months ago

case capitalize the first character in the sentence​

Answers

Answered by HARSHA5005
1

Answer:

The biggest festival for programmers "Geek Week" is back, Enter Now!

search

Sign In

Home

Courses

Hire With Us

Algorithmskeyboard_arrow_down

Data Structureskeyboard_arrow_down

Languageskeyboard_arrow_down

Interview Cornerkeyboard_arrow_down

GATEkeyboard_arrow_down

CS Subjectskeyboard_arrow_down

Studentkeyboard_arrow_down

GBlog

Puzzles

What's New ?

Program to convert first character uppercase in a sentence

Last Updated: 24-04-2020

Write a Java program to convert first character uppercase in a sentence and if apart from the first character if any other character is in Uppercase then convert into Lowercase?

Examples:

Input : gEEKs

Output :Geeks

Input :GFG

Output :Gfg

Input : GeeksforGeeks

Output : Geeksforgeeks

Method 1:

Java

// Java program to convert first character

// uppercase in a sentence

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;

}

public static void main(String[] args)

{

String str = "gEEks fOr GeeKs";

System.out.println(convert(str));

}

}

C#

Output:

Geeks For Geeks

Method 2 : Using Java Inbuilt methods

To capitalize each word, call the toLowerCase() method to put the string in lowercase format. Iterate the string, if any space if found in previous iteration and current element is not space then call the toUpperCase() method to put the first letter of word in uppercase format and append the string in buffer.

Below is the implementation:

Explanation:

We can create a list of all the words in a sentence (say string_array). After creating string_array, if we directly work upon that list and try to change the lowercase starting characters of words, we will get an error ” ‘str’ object does not support item assignment“. To avoid this we can create a list of ASCII values (say ascii_array) of characters corresponding to the characters in string_array.

Answered by anujrajput7904
0

Answer:

Explanation:

Uppercase

Similar questions