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
Solved

Shifting Elements in a String C#

Posted on 2006-11-06
11
4,322 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

860 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