- 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
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' && 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.
using System; class Demo { static String MyApplication(String str) { char []val = str.ToCharArray(); for (int i = 0; i < str.Length; i++) { if (i == 0 && val[i] != ' ' || val[i] != ' ' && val[i - 1] == ' ') { if (val[i] >= 'a' && val[i] <= 'z') { val[i] = (char)(val[i] - 'a' + 'A'); } } else if (val[i] >= 'A' && 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!
Advertisements