Solved

Shifting Elements in a String C#

Posted on 2006-11-06
11
4,340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBSCRIPT string remove all characters to the right after extension 4 71
Using Third Party DLL with Access VBA 14 51
BATCH to EXE Converter 2 67
Ruby or Python 7 49
A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

762 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