C# – Test String for Palindrome

email me

using System;

class Program
{
public static void Main()
{
string userString;

Console.WriteLine("Enter a word:\n");
userString = Convert.ToString(Console.ReadLine());
Console.WriteLine("\nWord is = {0}", userString);

string revString = Reverse(userString);
Console.WriteLine("Its reverse is = {0}", revString);

if (userString == revString)
Console.WriteLine("\nWord is a palindrome.\n");
else
Console.WriteLine("\nWord is not a palindrome.\n");

Console.ReadLine();
}

public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}


Output


Notes

Some further research for you…measuring the efficiency of different reverse methods.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public static string ReverseUsingArrayClass(string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}

public static string ReverseUsingCharacterBuffer(string text)
{
char[] charArray = new char[text.Length];
int inputStrLength = text.Length - 1;
for (int idx = 0; idx <= inputStrLength; idx++) { charArray[idx] = text[inputStrLength - idx]; } return new string(charArray); } public static string ReverseUsingStringBuilder(string text) { if (string.IsNullOrEmpty(text)) { return text; } StringBuilder builder = new StringBuilder(text.Length); for (int i = text.Length - 1; i >= 0; i--)
{
builder.Append(text[i]);
}

return builder.ToString();
}

private static string ReverseUsingStack(string input)
{
Stack<char> resultStack = new Stack<char>();
foreach (char c in input)
{
resultStack.Push(c);
}

StringBuilder sb = new StringBuilder();
while (resultStack.Count > 0)
{
sb.Append(resultStack.Pop());
}
return sb.ToString();
}

public static string ReverseUsingXOR(string text)
{
char[] charArray = text.ToCharArray();
int length = text.Length - 1;
for (int i = 0; i < length; i++, length--)
{
charArray[i] ^= charArray[length];
charArray[length] ^= charArray[i];
charArray[i] ^= charArray[length];
}

return new string(charArray);
}

static void Main(string[] args)
{
int cycleCount = 20;

string testString = string.Join(";", new string[] {
new string('l', 10),
new string('e', 10),
new string('v', 10),
new string('e', 10),
new string('l', 10),
});

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingCharacterBuffer(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingCharacterBuffer: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingArrayClass(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingArrayClass: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingStringBuilder(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStringBuilder: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingStack(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStack: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingXOR(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingXOR: " + stopwatch.ElapsedMilliseconds + "ms\n");

Console.ReadKey();
}
}
}

C++ String Rotation

email me
#include "stdafx.h"
#include <iostream>

using namespace std;

void Rotate(string s, int len);

int main() {
char s[20];

// Input
cout << "Enter string: "; cin >> s;
string t = s;

if (t.length() != 0)
{
cout << "\n------------\n";
Rotate(t, t.length());
cout << "\n------------";
}
else
cout << "Missing input!";

getchar(); getchar();

return 0;
}

void Rotate(string s, int len)
{
static int i = 0;
cout << "\"";

for (int j = i; j<len; ++j)
{
cout << s.at(j);
}

for (int k = 0; k<i; ++k)
{
cout << s.at(k);
}
cout << "\"";
i++;

if (i<len)
{
cout << endl;
Rotate(s, len);
}
}


Output

SCCM – Fix Software Center Shortcut

email me

After client update, Software Center shortcut is pointing to old/wrong location. Fix by running this Powershell Script.

$linkPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft System Center\Configuration Manager\Software Center.lnk"
$object = New-Object -COM WScript.Shell
$shortcut = $object.CreateShortcut($linkPath)
$shortcut.TargetPath = "C:\Windows\CCM\ClientUX\SCClient.exe"
$shortcut.Save()

Azure – CLI – Create Website in Azure Cloud Shell

email me

Azure Cloud Shell?

Create Website

From Cloud Shell, run this az vm extension set command to download and execute a PowerShell script that installs IIS and configures a basic home page.

az vm extension set \
--resource-group 79fdb1a1-c11e-4716-8a08-f9c4d710ed47 \
--vm-name myVM \
--name CustomScriptExtension \
--publisher Microsoft.Compute \
--settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-iis.ps1"]}' \
--protected-settings '{"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File configure-iis.ps1"}'

 

Set up Firewall

Run this az vm open-port command to open port 80 (HTTP) through the firewall.

az vm open-port \
--name myVM \
--resource-group 79fdb1a1-c11e-4716-8a08-f9c4d710ed47 \
--port 80

 

Verify Site

az vm show \
--name myVM \
--resource-group 79fdb1a1-c11e-4716-8a08-f9c4d710ed47 \
--show-details \
--query [publicIps] \
--output tsv

 

Check the page in a browser…

 

Notes

Install the Azure CLI on Windows

Azure Az Commands

Official Documentation by Microsoft

Azure – CLI – Create VM in Azure Cloud Shell

email me

Azure Cloud Shell?

Create VM

USERNAME=azureuser
PASSWORD=$(openssl rand -base64 32)

az vm create \
--name myVM \
--resource-group 79fdb1a1-c11e-4716-8a08-f9c4d710ed47 \
--image Win2016Datacenter \
--size Standard_DS2_v2 \
--location eastus \
--admin-username $USERNAME \
--admin-password $PASSWORD


Verify VM

az vm get-instance-view \
--name myVM \
--resource-group 79fdb1a1-c11e-4716-8a08-f9c4d710ed47 \
--output table

Output

 

Notes

Install Azure CLI for Windows

Azure Az Commands

Official Documentation by Microsoft

Microsoft Quickstart: Create a Windows virtual machine with the Azure CLI

Java Client 8 – 201 and 202

email me

The latest Java Client is available here:
https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

 

Silent Installation

JavaDownload.exe INSTALL_SILENT=1 STATIC=0 AUTO_UPDATE=0 WEB_JAVA=1 WEB_JAVA_SECURITY_LEVEL=H WEB_ANALYTICS=0 EULA=0 REBOOT=0

 

64 Bit

Version
8.0.2020.8

Size
73.6 MB

Registry
REG.exe ADD “HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F64180202F0}” /v “Publisher” /t REG_SZ /d “YourDesktopManagementSoftware—example: SCCM” /f

Uninstall
msiexec /x{26A24AE4-039D-4CA4-87B4-2F64180202F0}


32 Bit

Version
8.0.2020.8

Size
65.7 MB

Registry
REG.exe ADD “HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F32180202F0}” /v “Publisher” /t REG_SZ /d “YourDesktopManagementSoftware—example: SCCM” /f

Uninstall
msiexec /x{26A24AE4-039D-4CA4-87B4-2F32180202F0}

 

Windows – Add Space to C: Drive, though D: exists

email me

The problem is this…you want to add unallocated space to your C: drive, but you also have a D: drive you want to keep; this means the unallocated space is at the end of the disk tracker, and the extend option isn’t available to C: drive. According to Microsoft, you must delete other volumes, and then use the adjoining unallocated space to extend C: drive. Yikes. No.

C: D: drives and the unallocated space

C: drive with the grayed out Extend Volume option

 

Solution 

The answer is simple, don’t delete existing volumes—use a third party app to help you rearrange existing space.

First, download EaseUS Partition Master: https://www.easeus.com/partition-master/extend-c-drive-windows-10.html  mirror

Next, use the app to make sure you have unallocated space. Shrink a volume, if necessary.

And then select DISK 0 and the relative partition on the tracker (may be a recovery partition or data partition). Move drive letter partition(s) to the end. You’ll notice a shift in unallocated space. This is what you want.

 

Finally, apply changes by clicking the Execute Operation button.

 

Reboot computer, partition changes will be applied, and once back at the desktop, check disk management. BAM! You have successfully extended your C: drive, without having to delete volumes.

C++ Determine Prime Numbers in Array

email me

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>    // std::min_element, std::max_element

using namespace std;

int primeReturn(int arr[], int n)
{
int max_val = *max_element(arr, arr + n);

vector<bool> prime(max_val + 1, true);

prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++) {

if (prime[p] == true) {
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}

// Find primes
int primes;
string x;

for (int i = 0; i < n; i++)
if (prime[arr[i]])
x = x + std::to_string(arr[i]);
primes = std::stoi(x);
return primes;
}

int main(void)
{
// the array
int arr[] = { 9,8,5,3,2,4 };

// determine size of array
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Find primes in: 985324";

// output primes
cout << "\nPrimes: " << primeReturn(arr, n);

//system("pause"); // for visual studio
getchar();
}


Output