Strings manipulation C# Exercise - C# Programming Course

 Exercise

Strings manipulation

 Objetive

Write a C# program that asks the user for a string and:

- Replace all lowercase A by uppercase A, except if they are preceded with a space
- Display the initials (first letter and those after a space)
- Display odd letters uppercase and even letter lowercase

The program must display all generated strings.

 Example Code

using System;  // Importing the System namespace to use its functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        Console.Write("Enter a string: ");  // Prompt the user to input a string
        string input = Console.ReadLine();  // Read the input from the user

        // Call the methods to manipulate and display the string
        string replacedString = ReplaceLowercaseA(input);
        string initials = GetInitials(input);
        string oddEvenString = OddEvenCase(input);

        // Display all the manipulated strings
        Console.WriteLine($"Replaced String: {replacedString}");  // Display the string with replaced characters
        Console.WriteLine($"Initials: {initials}");  // Display the initials
        Console.WriteLine($"Odd/Even Case: {oddEvenString}");  // Display the odd/even case transformation
    }

    // Method to replace all lowercase 'a' by uppercase 'A', except when preceded by a space
    static string ReplaceLowercaseA(string text)
    {
        char[] chars = text.ToCharArray();  // Convert the string to a character array
        for (int i = 0; i < chars.Length; i++)
        {
            // Check if the current character is 'a' and not preceded by a space
            if (chars[i] == 'a' && (i == 0 || chars[i - 1] != ' '))
            {
                chars[i] = 'A';  // Replace 'a' with 'A'
            }
        }
        return new string(chars);  // Convert the modified character array back to a string
    }

    // Method to extract the initials (first letter and those after a space)
    static string GetInitials(string text)
    {
        string initials = string.Empty;  // Initialize an empty string for initials

        // Loop through each character of the input text
        for (int i = 0; i < text.Length; i++)
        {
            if (i == 0 || text[i - 1] == ' ')  // If it's the first character or follows a space
            {
                initials += text[i];  // Add the character to the initials string
            }
        }
        return initials;  // Return the initials string
    }

    // Method to display odd-index letters in uppercase and even-index letters in lowercase
    static string OddEvenCase(string text)
    {
        char[] chars = text.ToCharArray();  // Convert the string to a character array
        for (int i = 0; i < chars.Length; i++)
        {
            if (i % 2 == 0)  // Even index (0, 2, 4, ...)
            {
                chars[i] = Char.ToLower(chars[i]);  // Make even-index letters lowercase
            }
            else  // Odd index (1, 3, 5, ...)
            {
                chars[i] = Char.ToUpper(chars[i]);  // Make odd-index letters uppercase
            }
        }
        return new string(chars);  // Convert the modified character array back to a string
    }
}

More C# Exercises of Arrays, Structures and Strings

 Reverse array
Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he ...
 Array of even numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Write a C# program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must...
 Two dimensional array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Statistics V2
Write a C# statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been en...
 Struct
Write a C# Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Write a C# program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the use...
 Array of struct and menu
Write a C# program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for...
 Books database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
 Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
 Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
 Centered triangle
Write a C# program that Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Write a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Nested structs
Write a C# Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month an...
 Sort data
Write a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Write a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the...
 Two dimensional array 2: circunference on screen
Write a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it o...
 Computer programs
Write a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
 Exercise tasks
Write a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month and...
 Household accounts
Write a C# program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), sh...

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.