Strings Manipulation - C# Programming Exercise

This Exercise in C# involves creating a program that asks the user for a text string and performs three specific transformations on it. The program must:

1. Replace all lowercase 'a' letters with uppercase 'A', except when they are preceded by a space.
2. Display the initials of each word in the string (the first letter and those following a space).
3. Display the string alternating between uppercase letters at odd positions and lowercase letters at even positions.

The goal of this Exercise is to practice handling text strings and performing transformations in C#, as well as using loops and conditions to make specific changes to a string. Additionally, this Exercise strengthens the understanding of using spaces as delimiters to detect initials and manipulating characters at specific positions.

In this Exercise, the program will display all the strings generated after each transformation, allowing the user to see how the letters are modified throughout the process. This type of exercise is great for enhancing your C# skills and working with conditional logic and string manipulation.

 Category

Arrays, Structures and Strings

 Exercise

Strings Manipulation

 Objective

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.

 Write Your C# Exercise

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
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Nested structs

    This Exercise in C# involves creating a struct to store two pieces of data for a person: their name and their date of birth. The date of birth...

  •  Sort data

    This Exercise in C# involves creating a program that asks the user for 10 integer numbers (from -1000 to 1000), sorts them, and then displays them in ascending...

  •  Two dimensional array as buffer for screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions withi...

  •  Two dimensional array 2: circunference on screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...

  •  Computer programs

    Exercise in C# involves creating a program that can store up to 1000 records of computer programs. For each program, the following data should be stored: name,...

  •  Exercise tasks

    This exercise in C# consists of creating a program that can store up to 2000 "to-do tasks". Each task must store the following data: • Date (a set of 3 data: ...