-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.cs
More file actions
49 lines (40 loc) · 1.46 KB
/
ReverseString.cs
File metadata and controls
49 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
// Author: Jonathan Scholl
// Date: 10/27/2021
// Project: Reverse the character of a string
*/
using System;
namespace ReverseString
{
class Program
{
static void Main(string[] args)
{
//variables
string userInputString;
//prompt user input and store
Console.Write("Please input a string: ");
userInputString = Console.ReadLine();
//solution 1 (adds the chars from one char array in reverse order to the new char array):
//create array of same length and reverse string
char[] stringToCharArray = userInputString.ToCharArray();
char[] reverseCharArray = new char[stringToCharArray.Length];
for (int x = 0; x < stringToCharArray.Length; x++)
{
reverseCharArray[x] = stringToCharArray[stringToCharArray.Length - 1 - x];
}
//output reversed string
for(int x = 0; x < reverseCharArray.Length; x++)
{
Console.Write(reverseCharArray[x]);
}
//solution 2 (adds invidual chars to a string in reverse order and output the string):
string reverseString = "";
for (int x = 0; x < stringToCharArray.Length; x++)
{
reverseString += stringToCharArray[stringToCharArray.Length - 1 - x];
}
Console.WriteLine("\n" + reverseString);
}
}
}