473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help Please...Sub Cipher C#...Arrays and nested for loops

I am a C# begginer.I have to write a program that asks the user to enter a word,then it encodes it and after encoding it ,it decodes it again(by using Arrays to make sure that weget the right answer).
my code runs the encoding part ,but the decoding it outputs is not the same as what user enters ,Can you please fix the code for me(I have been working on it for 2 days now ,and no results:( )
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class YourName
  3. {
  4.     public static void Main(string[] arg)
  5.     {
  6.         Console.WriteLine("Enter the word you want to be Encoded?");
  7.         string name = Console.ReadLine();
  8.         char[] original ={ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  9.         char[] encode ={ 't', 'm', 'e', 's', 'f', 'k', 'j', 'a', 'x', 'n', 'o', 'v', 'l', 'u', 'c', 'h', 'z', 'g', 'y', 'b', 'w', 'p', 'd', 'r', 'i', 'q' };
  10.         char[] lastArray = encode;
  11.         int len = name.Length;
  12.         Console.Write("Encoded word  :");
  13.         for (int i = 0; i < len; i++)
  14.         {
  15.             for (int j = 0; j < 26; j++)
  16.             {
  17.                 if (name[i] == original[j])
  18.                 {
  19.                     Console.Write(encode[j]);
  20.                     encode[j] = lastArray[i];
  21.                 }
  22.             }
  23.         }
  24.         Console.WriteLine();
  25.         for (int m = 0; m < len; m++)
  26.         {
  27.             for (int n = 0; n < 26; n++)
  28.             {
  29.                 if (lastArray[m] == encode[n])
  30.                 {
  31.                     Console.Write(original[n]);
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
  37.  
Feb 12 '09 #1
1 1930
Plater
7,872 Expert 4TB
Edit: I take that back, you have all kindsa problems

This line:
encode[j] = lastArray[i];

You are overwriting the value of encode with lastArray, I think you want it the other way around?

This line:
char[] lastArray = encode;
You are not actually creating a new array, you are making lastArray and encode point to the same 26 element character array. If your user typed a word with more then 26 letters, you would get index out of bounds exceptions.
You should create lastArray with the same amount of elements as there are characters in the string name
Feb 12 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bart Nessux | last post by:
How is this done in php? I've tried several variations of the following: contact_array = ($_POST, $_POST, $_POST, $_POST, $_POST); The ultimate goal is to insert the array into a table in...
25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
4
by: Josh | last post by:
Howdy i am newb somewhat to programing and i was just for fun trying to compile a program that asks the user for an odd int less than 22 and then returns this ***************** ******* *********...
0
by: Paolo Tavalazzi | last post by:
I have a problem on FROM subselect that i don't understand. I do two query different only for a WHERE clause in a FROM subquery . 1) explain analyze SELECT DISTINCT ON...
3
by: tzuriel | last post by:
Hello all, I think nested loops will do what I want, but I can't seem to get them right. I have two tables, members and casts. I run a query as follows: $sql_query="SELECT DISTINCT...
2
by: theronnightstar | last post by:
I am writing an anagram program for my fiance. Figured it would be an excellent task to learn from. The way it is supposed to work is it reads in a word list from a file into a temporary...
1
by: pstuning | last post by:
if you see the cost of QUERY time in below joining of table and 2 views we can easily say it is taking a lot of time. This is a production support issue and i need some help. SELECT...
3
by: dragonslayer008 | last post by:
instead of using 2D arrays: int A; I heard it was faster/preferred to use a 1D array to simulate: int A; Can someone explain why? Is it merely the double dereference you need to do with...
1
by: ckking | last post by:
ok the part that i had earlier was supposed to part of this cipher, im having trouble with it not converting spaces char ch; char inputfile; cout << "enter input filename:"<<endl;...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.