You must Sign In to post a response.
  • Category: ASP.NET

    Program to convert first letter of each word into upper / lower case from the given string

    program to convert first letter of each word into upper / lower case from the given string.

    e.g. input = "hello welcome to Dotnetspider",
    output = "Hello Welcome To dotnetspider"
  • #762918

    Hello Sai krishna,

    Refer the below code :

    static string UppercaseWords(string value)
    {
    char[] array = value.ToCharArray();
    // Handle the first letter in the string.
    if (array.Length >= 1)
    {
    if (char.IsLower(array[0]))
    {
    array[0] = char.ToUpper(array[0]);
    }
    }
    // Scan through the letters, checking for spaces.
    // ... Uppercase the lowercase letters following spaces.
    for (int i = 1; i < array.Length; i++)
    {
    if (array[i - 1] == ' ')
    {
    if (char.IsLower(array[i]))
    {
    array[i] = char.ToUpper(array[i]);
    }
    else if (char.IsUpper(array[i]))
    {
    array[i] = char.ToLower(array[i]);
    }
    }
    }
    return new string(array);
    }

    private void btnConvert_Click(object sender, EventArgs e)
    {
    txtOutput.Text = UppercaseWords(txtInput.Text);
    }


    See the attached image. It's an output i have got using above code and i am damn sure this is what you are looking for.


    Hope this will help you.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762920
    Hi Nirav Lalan.

    Thanks for your reply.
    Can you make it with less amount of code.

    Regards

    Sai Krishna Reddy.

  • #762924
    Hello Sai Krishna,

    I can try but it will be bit tough to read the string and take first letter and than check whether it is in uppercase or lowercase.

    You can also try if it is possible.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762945
    Hai Sai Krishna,
    We can make use of the inbuilt methods to do this as below:

    string inputString = "hello welcome to Dotnetspider";
    string[] inputVal= inputString.Split(' ');
    StringBuilder sb = new StringBuilder();
    foreach (var item in inputVal)
    {
    var firstLetter = char.IsUpper(Convert.ToChar(item.First().ToString())) ? item.First().ToString().ToLower() : item.First().ToString().ToUpper();
    sb.Append(firstLetter + item.ToString().Substring(1));
    sb.Append(" ");
    }
    Console.WriteLine(sb.ToString());

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #763085
    private string ChangeCase(string text)
    {
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    return textInfo.ToTitleCase(text.ToLower());
    }

  • #763096
    Hello Manoj Verma,

    The code you have provided which doesn't satisfy Sai Krishna's requirement. He means to say that if the first letter of word is in upper case than it should be converted in lower case else it should be converted in upper case if it is in lower case. So please read the question carefully than only provide the answer.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments