I've just started programming in C#. I'm trying to build a simple Vigenere text encryption tool as a personal project.
My problem should be very easy to fix, yet it's really stressing me out to find the error. In my code I'm trying to do a simple check to see whether or not the character in my string is a space; I have set up my if statement properly yet it is skipping the first test and moving to the else if, even when the first test is true. Id really like some help on this one.
My problem area is at the bottom.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class fun2013
{
public static void Main()
{
Console.WriteLine("fun 2013");
string UserName;
do
{
Console.Write("LOGIN//: ");
UserName = Console.ReadLine();
}
while(UserName != "Max");
Console.WriteLine(("Hello ") + (UserName) + (", enter your key below."));
//USER ENTERS TEXT AT THIS POINT
string loweredPass = Console.ReadLine().ToLower();
Console.WriteLine("Changing CASE...");
Console.WriteLine(loweredPass);
string Strippedpass = loweredPass.Replace(" ","");
Console.WriteLine("STRIPPING SPACES...");
Console.WriteLine(Strippedpass);
int passLength = Strippedpass.Length;
Console.WriteLine("Enter your message below.");
string userMessage = Console.ReadLine();
int MessageLength = userMessage.Length;
//BEGIN PROCESSING STRINGS INTO ARRAYS
string temp = "";
StringBuilder bcon = new StringBuilder();
char [] passArray = Strippedpass.ToCharArray();
char [] messArray = userMessage.ToCharArray();
string letterf = "";
for(int i=0, j=0; j < (MessageLength); i++, j++) //i used for key array, j used for message length
{
>>> if (messArray[i].ToString() == " ")
{
letterf = " ";
}
else if (messArray[i].ToString() != " ")
{
letterf = passArray[i].ToString();
}
if (i >= (passLength-1)) //array starts at value 0, length check starts at 1. Subtract 1 to keep them equal
{i = -1;} //-1 is used so it will go back to value of 0 on next loop
temp = letterf;
bcon.Append(temp);
}
Console.WriteLine();
Console.WriteLine(bcon);
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine(); //KILL APPLICATION
}
}
Thanks for the help everyone, but after further inspection I noticed I made an error in my for loop. I was resetting the message array reader using the same interval as the key array (int i). I changed it to use the correct integer, "j". I also put the "temp" string updater and string builder into each if statement in the loop. It's now running correctly.
for (int i=0, j=0; j < (MessageLength); i++, j++) //i used for key array, j used for message length
{
if (messArray[j].ToString() != " ")
{
letterf = passArray[i].ToString();
temp = letterf;
bcon.Append(temp);
}
else if (messArray[j].ToString() == " ")
{
letterf = " ";
temp = letterf;
bcon.Append(temp);
}
if (i >= (passLength-1)) //array starts at value 0, length check starts at 1. Subtract 1 to keep them equal
{i = -1;} //-1 is used so it will go back to value of 0 on next loop
}
Char.IsWhiteSpace(char)
See also the String.IsNullOrEmpty
or String.IsNullOrWhiteSpace
.
I'm trying to do a simple check to see whether or not the character in my string is a space;
You can change this code
messArray[i].ToString() != " "
to
char.IsWhiteSpace(messArray[i])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With