w3resource

C#: Create a new string where the first 4 characters will be in lower case


First 4 Chars Lowercase, Rest Uppercase

Write a C# program to create a string where the first 4 characters are in lower case. If the string is less than 4 letters, make the whole string in upper case.

Pictorial Presentation:

>C# Sharp Exercises: Create a new string where the first 4 characters will be in lower case

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Exercise42
{
    public static void Main( )
    {
        // Prompt the user to input a string
        Console.Write("Input a string: ");

        // Read the input string and store it in the variable 'str'
        string str = Console.ReadLine();

        // Check if the length of the input string is less than 4 characters
        if (str.Length < 4) 
            // If the length is less than 4, convert the whole string to uppercase and display it
            Console.WriteLine(str.ToUpper());
        else
            // If the length is 4 or greater, convert the first 4 characters to lowercase
            // and concatenate the rest of the string as is, then display the modified string
            Console.WriteLine(str.Substring(0, 4).ToLower() + str.Substring(4, str.Length - 4));
    }
}

Sample Output:

Input a string: w3r                                                    
W3R

Flowchart:

Flowchart: C# Sharp Exercises - Create a new string where the first 4 characters will be in lower case

For more Practice: Solve these Related Problems:

  • Write a C# program to reverse a string and apply different cases to the first 4 and remaining characters.
  • Write a C# program to capitalize the last 4 characters of a string and make the rest lowercase.
  • Write a C# program to convert even-index characters to lowercase and odd-index characters to uppercase.
  • Write a C# program to make the first 4 characters lowercase unless the string has less than 4 characters.

Go to:


PREV : Check 'w' Appears 1-3 Times.
NEXT : Check String Starts with 'www'.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.