Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shifting a String in C#

  static void Main(string[] args)
  {
     string s = "ABCDEFGH";
     string newS = ShiftString(s);
     Console.WriteLine(newS);
  }
  public static string ShiftString(string t)
  {
     char[] c = t.ToCharArray();
     char save = c[0];
     for (int i = 0; i < c.Length; i++)
     {
        if (c[i] != c[0])
        c[i] = c[i - 1];
     }
     Console.WriteLine(c);
     String s = new string(c);
     return s;
  }

I need to shift the string s one space to the left, so i end up with the string: "BCDEFGHA" So i thought about changing the string into a char array and work my way from there, but im not sure how to succesfully make this work. I'm pretty certain i need a for loop, but i need some help on how to shift the char sequence one space to the left.

like image 265
user2104751 Avatar asked Feb 24 '13 15:02

user2104751


People also ask

How do you shift a string?

A shift operation will remove the first character of a string and add the same character at the end of that string. For example after you perform a shift operation on a string 'abcd', the new string will be 'bcda'. If you perform this operation two times, the new string will be 'cdab'.

How do you shift a string in C++?

So if the string is “abc” and shifts = [3,5,9], then after shifting the first 1 letter of S by 3, will have “dbc”, shifting first two letters of S by 5, we have “igc”, and shifting first 3 letters of S by 9, we have “rpl”, and this is the answer.

What is << in C?

The << operator shifts the left-hand value left by the (right-hand value) bits. Your example does nothing! 1 shifted 0 bits to the left is still 1. However, 1 << 1 is 2, 1 << 2 is 4, etc.

How do you shift bits to the left in C?

The bits of first operand are shifted to the left by the number of positions specified by the second operand. The syntax for left shift operator in C is as follows:

How do you shift a string by x times?

shift [i] = x, shift the first i+1 letters of input string by x times. Traverse the Shift array from the end (n-1, where n is the length of an array) to start (index 0).

What is the use of a shift function in C++?

It is used to shift the bits of a value to the left by adding zeroes to the empty spaces created at the right side after shifting.

What is left shift operator in C?

Left shift operator is a bitwise shift operator in C which operates on bits. It is a binary operator which means it requires two operands to work on. Following are some important points regarding Left shift operator in C:


2 Answers

In C# 8 and above...

Rotate Right by one...

t = myString[^1] + myString[..^1];

Or, Rotate Left by one...

t = myString[1..] + myString[0];

Or, Rotate Right by an amount...

t = myString[^amount..] + myString[..^amount];

Or, Rotate Left by an amount...

t = myString[amount..] + myString[..amount];
like image 104
SunsetQuest Avatar answered Sep 19 '22 05:09

SunsetQuest


Using Span<T> we can significantly improve the performance. E.g.:

public static class StringExtensions
{
    public static string ShiftString(this string s)
    {
        return string.Concat(s.AsSpan(1), s.AsSpan(0, 1));
    }
}

Benchmark

This answer + existing answers. Using spans, the shifting of the string becomes both faster and produces less garbage.

|  Method |     Data |     Mean |   Median | Allocated |
|-------- |--------- |---------:|---------:|----------:|
|    L33t | ABCDEFGH | 18.56 ns | 18.21 ns |      40 B |
|  Zs2020 | ABCDEFGH | 36.04 ns | 35.20 ns |      96 B |
| JohnWoo | ABCDEFGH | 47.69 ns | 47.39 ns |     104 B |
|  AlinaB | ABCDEFGH | 52.56 ns | 52.07 ns |     104 B |

Full test code

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Running;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<StringShiftTest>();
        }
    }

    [Config(typeof(Config))]
    public class StringShiftTest
    {
        private class Config : ManualConfig
        {
            public Config() => AddDiagnoser(MemoryDiagnoser.Default, new EtwProfiler());
        }

        [Params("ABCDEFGH")]
        public string Data;

        [Benchmark]
        public string L33t() => string.Concat(Data.AsSpan(1), Data.AsSpan(0, 1));

        [Benchmark]
        public string Zs2020() => (Data + Data).Substring(1, Data.Length);

        [Benchmark]
        public string JohnWoo() => Data.Substring(1, Data.Length - 1) + Data.Substring(0, 1);

        [Benchmark]
        public string AlinaB() => Data.Remove(0, 1) + Data.Substring(0, 1);
    }
}
like image 25
l33t Avatar answered Sep 19 '22 05:09

l33t