w3resource

C#: Separate the individual characters from a string

C# Sharp String: Exercise-3 with Solution

Write a C# Sharp program to separate individual characters from a string.

C# Sharp Exercises: Separate the individual characters from a string

Sample Solution:-

C# Sharp Code:

using System;

// Define the Exercise3 class
public class Exercise3
{
    // Main method - entry point of the program
    public static void Main()
    {
        string str; // Declare a string variable
        int l = 0; // Initialize a variable to traverse the string characters

        // Prompt the user to separate individual characters from a string
        Console.Write("\n\nSeparate the individual characters from a string:\n");
        Console.Write("------------------------------------------------------\n");

        // Request user input for a string
        Console.Write("Input the string: ");
        str = Console.ReadLine(); // Read the user input

        Console.Write("The characters of the string are: ");

        // Loop through the string to separate and display individual characters
        while (l <= str.Length - 1)
        {
            Console.Write("{0} ", str[l]); // Display each character with a space
            l++; // Move to the next character in the string
        }

        Console.Write("\n\n"); // Add newline for formatting
    }
}

Sample Output:

Separate the individual characters from a string :                                                            
------------------------------------------------------                                                        
Input the string : w3resource.com                                                                             
The characters of the string are  :  w 3 r e s o u r c e . c o m 

Flowchart:

Flowchart: Separate the individual characters from a string.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find the length of a string without using library function.
Next: Write a program in C# Sharp to print individual characters of the string in reverse order.

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.