Link to home
Start Free TrialLog in
Avatar of jmkotman
jmkotmanFlag for United States of America

asked on

Shifting Elements in a String C#

I need to know how you shift the element of a string N places.  So if the string is string s = DPNQVUFS!TDJFODF; and I want to shift it one place to the right i should get "FDPNQVUFS!TDJFOD"
Avatar of VoteyDisciple
VoteyDisciple

How about...
String s = "abcde";
s = s.Substring(n) + s.Substring(0, n)
I may be off by a +1 or a -1 in there, since no matter how many times I write substring expressions I never actually manage to nail it on the first shot.  I think that's correct, but give it a try.  (-:
Avatar of jmkotman

ASKER

That worked perfectly but now i also need to shift it the other direction, how is that done?
Just defining n = s.Length - n  should handle that.  If you have a length 10 string and you move 1 character from the beginning to the end, that's equivalent to moving 9 characters from the end to the beginning.
ok i tried that its not seeming to work.  This is what im trying to do in my program.  I read in a file which is listed below: Also the File needs to be called input.txt when you run it.

e,COMPUTER SCIENCE,1
e,HELLO WORLD,11
e,THIS IS JUST A TEST,8
d,FDPNQVUFS!TDJFOD,1
d,KZZKXYIGVOZGR&R,6
d,TSHTXLT%GW,5

ok once that is read in it encryps that middle string using ASCII keys.  If you run the program you will see the answer I get.  The correct answers is:

Plain text: COMPUTER SCIENCE
Ciphertext: FDPNQVUFS!TDJFOD

Plain text: HELLO WORLD
Ciphertext: SPWWZ+bZ]WO

Plain text: THIS IS JUST A TEST
Ciphertext: \(I(\M[\\PQ[(Q[(R][

Ciphertext: FDPNQVUFS!TDJFOD
Plain text: COMPUTER SCIENCE

Ciphertext: KZZKXYIGVOZGR&R
Plain text: CAPITAL LETTERS

Ciphertext: TSHTXLT%GW
Plain text: GO BRONCOS


Code:

using System;
using System.IO;

namespace Lab07_Encryption
{
      class Class1
      {
            static string encryption;
            static string message;
            static int shift;
            static string encoded;
            static string decoded;

            [STAThread]
            static void Main(string[] args)
            {
                  ReadFile();
            }

            public static void ReadFile()
            {

                  StreamReader fileReader;
                  string fileName = "input.txt";
             
                  fileReader = new StreamReader(fileName);
                  string sLine;
      
                  sLine = fileReader.ReadLine();

                  string [] split;
                  const char delm = ',';

                  while(sLine != null)
                  {
                        split = sLine.Split(delm);

                        encryption = split[0];
                        message = split[1];
                        shift = int.Parse(split[2]);

                        string en = "e";

                        if(encryption == en)
                        {
                              encode(encoded);
                        }
                        else
                        {
                              decode(decoded);
                        }

                        sLine = fileReader.ReadLine();
                  }
            }

            public static void encode(string encoded)
            {      
                  char[] array = message.ToCharArray();

                  for (int i = 0; i < array.Length; i++)
                  {
                        int encodedChar = ((char)(int)array[i] + shift);
                        encoded += (char)encodedChar;
                  }

                  shift = encoded.Length - shift;

                  encoded = encoded.Substring(0, shift) + encoded.Substring(shift);

                  Console.WriteLine("Plain Text: {0}", message);
                  Console.WriteLine("Ciphertext: {0}", encoded);
                  Console.WriteLine();
            }

            public static void decode(string decoded)
            {
                  char[] array = message.ToCharArray();

                  for (int i = 0; i < array.Length; i++)
                  {
                        int decodedChar = ((int)array[i] - shift);
                        decoded += (char)decodedChar;
                  }

                  decoded = decoded.Substring(shift) + decoded.Substring(0, shift);

                  Console.WriteLine("Plain Text: {0}", message);
                  Console.WriteLine("Ciphertext: {0}", decoded);
                  Console.WriteLine();
            }
      }
}

other direction = s.Substring(0,n) + s.Substring(n), i.e. swap the two substrings.
my bad.
the original post by votey shifts the string to LEFT by n places. for right use n = length-n. i just realized, that is same as votey's second post. could you post what wrong answers you are getting?
ASKER CERTIFIED SOLUTION
Avatar of tang_tzuchi
tang_tzuchi
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When I run the program I get DPNQVUFS!TDJFODF and it needs to be shifted to I get FDPNQVUFS!TDJFOD.
A couple debugger questions, I think:

Does shift have the right value?  I'd check to see that those .Substring() statements are really returning the right thing.  Surely they're executing since I don't see any path of execution that skips over them, so I'd pull attention onto the "does shift get properly set" question.
it should be:

encoded = encoded.Substring(encoded.Length - shift) + encoded.Substring(0,encoded.Length - shift);


does this help you solve your problem?