C# Sharp Basic Algorithm Exercises: Create a new string of the characters at indexes 0,1, 4,5, 8,9 ... from a given string
C# Sharp Basic Algorithm: Exercise-37 with Solution
Write a C# Sharp program to create a new string of the characters at indexes 0,1, 4,5, 8,9 ... from a given string.
Pictorial Presentation:
Sample Solution:-
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(test("Python"));
Console.WriteLine(test("JavaScript"));
Console.WriteLine(test("HTML"));
Console.ReadLine();
}
public static string test(string str1)
{
var result = string.Empty;
for (var i = 0; i < str1.Length; i += 4)
{
var c = i + 2;
var n = 0;
n += c > str1.Length ? 1 : 2;
result += str1.Substring(i, n);
}
return result;
}
}
}
Sample Output:
Pyon JaScpt HT
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 from a give string where a specified character have been removed except starting and ending position of the given string.
Next: Write a C# Sharp program to count the number of two 5's are next to each other in an array of integers. Also count the situation where the second 5 is actually a 6.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework