C# Program to convert first character uppercase in a sentence



Let us say the following is your string −

String str = "Welcome to our website!";

Create a char array of the string included above using the ToCharArray() method:

char []ch = str.ToCharArray();

To convert the first character to uppercase −

if (ch[i] >= 'a' &amp;&amp; ch[i] <= 'z') {    // Convert into Upper-case    ch[i] = (char)(ch[i] - 'a' + 'A'); }

Example

You can try to run the following code to convert first character uppercase in a sentence.

Live Demo

using System; class Demo {    static String MyApplication(String str) {       char []val = str.ToCharArray();       for (int i = 0; i < str.Length; i++) {          if (i == 0 &amp;&amp; val[i] != ' ' ||             val[i] != ' ' &amp;&amp; val[i - 1] == ' ') {                if (val[i] >= 'a' &amp;&amp; val[i] <= 'z') {                   val[i] = (char)(val[i] - 'a' + 'A');                }             } else if (val[i] >= 'A' &amp;&amp; val[i] <= 'Z')                val[i] = (char)(val[i] + 'a' - 'A');       }       String s = new String(val);       return s;    }    public static void Main() {       String str = "Welcome to our website!";       Console.Write(MyApplication(str));    } }

Output

Welcome To Our Website!
Updated on: 2020-06-19T09:27:24+05:30

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements