C#: Create a string like 'aababcabcd' from a given string 'abcd'
C# Sharp Basic Algorithm: Exercise-30 with Solution
Cumulative Substrings
Write a C# Sharp program to create a string like 'aababcabcd' from a given string 'abcd'.
Visual Presentation:
Sample Solution:-
C# Sharp Code:
using System;
// Namespace declaration
namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Displaying the output of the 'test' method with different string inputs
Console.WriteLine(test("abcd")); // Output: aababcabcd
Console.WriteLine(test("abc")); // Output: aababc
Console.WriteLine(test("a")); // Output: a
Console.ReadLine(); // Keeping the console window open
}
// Method to create a new string by concatenating substrings from the input string
public static string test(string str)
{
var result = string.Empty; // Variable to store the resulting string
// Loop through the characters of the input string
for (var i = 0; i < str.Length; i++)
{
// Concatenate substrings of the input string from index 0 to 'i'
result += str.Substring(0, i + 1);
}
return result; // Return the concatenated string
}
}
}
Sample Output:
aababcabcd aababc a
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to create a new string made of every other character starting with the first from a given string.
Next: Write a C# Sharp program to count a substring of length 2 appears in a given string and also as the last 2 characters of the string. Do not count the end substring.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-30.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics