In: Computer Science
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT
import java.util.*;
public class TestPaperFolds
{
public static void main(String[] args)
{
for(int i = 1; i <= 4;
i++)
//loop for i = 1 to 4 folds
{
String
fold_string = paperFold(i); //call paperFold to get the
String for i folds
System.out.println("For " + i + " folds we get: " +
fold_string);
}
}
public static String paperFold(int
numOfFolds) //recursive function that returns the
FoldSequence string
{
if(numOfFolds ==
1){ //if numOfFolds = 1, the
return "v"
return
"v";
}
else{
//Otherwise make a recursive call to the paperFold(numOfFolds -
1)
String
s = paperFold(numOfFolds -1);
return
flipString(reverseString(s)) + "v" + s; //reverse
the string s, then flip it, then append "v" and s
}
}
public static String reverseString(String
s) //this helper function reverses a string
{
String out = "";
for(int i = s.length()-1; i
>= 0; i--)
out =
out + s.charAt(i);
return out;
}
public static String flipString(String s)
//this helper function flips a string
{
String out = "";
for(int i = 0; i <
s.length(); i++){
if(s.charAt(i) ==
'v'){
//converts v into ^
out = out + '^';
}
else
if(s.charAt(i) == '^'){
//converts ^ into v
out = out + 'v';
}
}
return out;
}
}
-----------
Sample Output:
For 1 folds we get: v
For 2 folds we get: ^vv
For 3 folds we get: ^^vv^vv
For 4 folds we get: ^^v^^vvv^^vv^vv
Dear Student ,
As per requirement submitted above kindly find below solution.
Here new console application in C# is created using visual studio 2019 with name "TestPaperFoldsApp". This application contains a program with name "TestPaperFolds.cs".Below is the details of this class.
TestPaperFolds.cs :
//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace TestPaperFoldsApp
{
class TestPaperFolds //C# class
{
//Main() method
static void Main(string[] args)
{
//using for loop
for (int i = 1; i <= 4; i++) //loop for i = 1 to 4 folds
{
string fold_string = paperFold(i); //call paperFold to get the
String for i folds
Console.WriteLine("For " + i + " folds we get: " +
fold_string);
}
}
public static string paperFold(int numOfFolds) //recursive
function that returns the FoldSequence string
{
if (numOfFolds == 1)
{ //if numOfFolds = 1, the return "v"
return "v";
}
else
{ //Otherwise make a recursive call to the paperFold(numOfFolds -
1)
String s = paperFold(numOfFolds - 1);
return flipString(reverseString(s)) + "v" + s; //reverse the string
s, then flip it, then append "v" and s
}
}
public static string reverseString(string s) //this helper function
reverses a string
{
string outVar = "";
for (int i = s.Length - 1; i >= 0; i--)
outVar = outVar + s[i];
return outVar;
}
public static string flipString(string s) //this helper function
flips a string
{
string outvar = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'v')
{ //converts v into ^
outvar = outvar + '^';
}
else if (s[i] == '^')
{ //converts ^ into v
outvar = outvar + 'v';
}
}
return outvar;
}
}
}
==================================
Output :Run application using F5 and will get the screen as shown below