Solved

Shifting Elements in a String C#

Posted on 2006-11-06
11
4,244 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now