Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shifting Elements in a String C#

Posted on 2006-11-06
11
Medium Priority
?
4,580 Views
Last Modified: 2008-01-09
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"
0
Comment
Question by:jmkotman
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17886636
How about...
String s = "abcde";
s = s.Substring(n) + s.Substring(0, n)
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17886638
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.  (-:
0
 

Author Comment

by:jmkotman
ID: 17886660
That worked perfectly but now i also need to shift it the other direction, how is that done?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17886667
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.
0
 

Author Comment

by:jmkotman
ID: 17886706
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();
            }
      }
}

0
 
LVL 9

Expert Comment

by:jhshukla
ID: 17887129
other direction = s.Substring(0,n) + s.Substring(n), i.e. swap the two substrings.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 17887153
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?
0
 
LVL 3

Accepted Solution

by:
tang_tzuchi earned 2000 total points
ID: 17887573
HI,

do not replace the shift value in the following statement:

 shift = encoded.Length - shift;

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


you should write like this:

shift = encoded.Length - shift; *delete this statement

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

Author Comment

by:jmkotman
ID: 17890753
When I run the program I get DPNQVUFS!TDJFODF and it needs to be shifted to I get FDPNQVUFS!TDJFOD.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17890837
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.
0
 
LVL 3

Expert Comment

by:tang_tzuchi
ID: 17894287
it should be:

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


does this help you solve your problem?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Six Sigma Control Plans
Progress

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question