Caesar Cipher Program - Display 'Invalid String' if the user enters a non-alphabetical character
https://github.com/AbdulRaheemNazir/MiniApps/blob/main/Encrypt%20Caesar%20Cipher
Hi,
I'm making a multifunctional program in C#. I have made a Caesar Cipher Encryption program and it works. However when I type in a string plus a non-alphabetical character to encrypt it encrypts the alphabetical values (which is good) and also displays the non-alphabetical character. I would want it so that if the user enters a non-alphabetical character in the string it will say to type a valid string but I'm unsure how to approach this. Any assistance would be greatly appreciated
GitHub
MiniApps/Encrypt Caesar Cipher at main · AbdulRaheemNazir/MiniApps
Contribute to AbdulRaheemNazir/MiniApps development by creating an account on GitHub.
39 Replies
RegEx probably
[a-zA-Z0-9]
will cover all alphanumerical charactersYes, but I would like only alphabetical characters not numberical
[a-zA-Z]
thenAnd I have no idea how to implement it so can you show me a e example of you have time
Regex.IsMatch Method (System.Text.RegularExpressions)
Indicates whether the regular expression finds a match in the input string.
I'm not very good with English and understanding code using Microsoft if you could help me learn how to implement that would be more helpful because I learn easier on Practicality if you could help me so
1. Create a new
Regex
object with the regex I gave you
2. Use .IsMatch()
to see if your string matches the regex
3. If it does, proceed. If it doesn't, handle it somehowIm still unsure how to add it in context with my code
regex - check if a string contains only alphabets c#
How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the...
I looked at this website and implemented my code here
Same code just added this
public bool IsAlphabets(string inputString)
{
Regex r = new Regex("^[a-zA-Z ]+$");
if (r.IsMatch(inputString))
return true;
else
return false;
}
However I get a red error line under regex
With the error message as shown
This is my first time creating a program verifying if the string contains only alphabets characters and I'm unsure how to add and not many tutorials out there
@AspNetCore
Why ASP.NET Core role?
I tried to tag the member my bad
You're missing the namespace import
Use quick fixes to import it
That has helped got rid of the error
However it still has the same issue as before
Well, you defined a local function
IsAlphabet
But are you using it?
I don't see you actually checking the inputstatic void Optionthree()
{
bool check = false;
Console.WriteLine("Type a string to encrypt:");
string UserString = Console.ReadLine();
bool IsAlphabets(string UserString)
{
Regex r = new Regex("^[a-zA-Z ]+$");
if (r.IsMatch(UserString))
return true;
else
return false;
Console.WriteLine("Invalid");
Optionthree();
}
Cool cool
I have here
You're still not using this local function
You never call it anywhere
You never get the output
You just defined it and called it a day
I used an if and else statement and I said if it is a character then nothing should happen however if not alphabet value it should display invalid
You never call the function
So it will never run
How would I call the functipn
Either get rid of the function and just check with the regex directly in your code
Or call the function
You really need to learn the basics first
$helloworld
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
Here's some tutorials that will help you get started with the basics of C#, like calling functions/methods
You call methods/functions... normally,
FunctionName()
You pass parameters if need be
And get the outputDo you mean like this
static void Optionthree()
{
bool check = false;
Console.WriteLine("Type a string to encrypt:");
string UserString = Console.ReadLine();
Regex r = new Regex("^[a-zA-Z ]+$");
if (r.IsMatch(UserString)) ;
for (int i = 0; i < UserString.Length; i++)
{
if (UserString[i] >= 'a' && UserString[i] <= 'z' || UserString[i] == ' ')
{
check = true;
continue;
}
else
{
check = false;
break;
}
}
if (check == false)
{
UserString = Console.ReadLine();
}
else
Console.WriteLine("Invalid");
For example, yes
I have changed my code but it does the same
Remove the semicolon here
is equal in function to
That's why I recommended you learn the basics first
Yes, I agree I normally make Websites, however, I would like to learn more about C# a friend recommended this as a first project to start but it hasn't been the easiest. I have looked at the Microsoft Links to Learning C# and I will start them soon. If you have any other links or tips I would appreciate it. However this is the last requirement of finishing the program so if you could help point me in the right direction I would really appreicate it
static void Optionthree()
{
bool check = false;
Console.WriteLine("Type a string to encrypt:");
string UserString = Console.ReadLine();
Regex r = new Regex("^[a-zA-Z ]+$");
if (r.IsMatch(UserString))
{
}
for (int i = 0; i < UserString.Length; i++)
{
if (UserString[i] >= 'a' && UserString[i] <= 'z' || UserString[i] == ' ')
{
check = true;
continue;
}
else
{
check = false;
break;
}
}
if (check == false)
{
UserString = Console.ReadLine();
}
else
{
Console.WriteLine("Invalid");
Optionthree();
}
It works but doesnt display invalid here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Caesar_Cipher
{
class Program
{
public static char cipher(char ch, int key)
{
if (!char.IsLetter(ch))
{
return ch;
}
char d = char.IsUpper(ch) ? 'A' : 'a';
return (char)((((ch + key) - d) % 26) + d);
}
public static string Encipher(string input, int key)
{
string output = string.Empty;
foreach (char ch in input)
output += cipher(ch, key);
return output;
}
public static string Decipher(string input, int key)
{
return Encipher(input, 26 - key);
}
static void Main(string[] args)
{
Console.WriteLine("Type a string to encrypt:");
string UserString = Console.ReadLine();
Regex r = new Regex("^[a-zA-Z ]+$");
if (r.IsMatch(UserString)) ;
{
Console.WriteLine("\n");
Console.Write("Enter your Key");
int key = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("Encrypted Data");
string cipherText = Encipher(UserString, key);
Console.WriteLine(cipherText);
Console.Write("\n");
Console.WriteLine("Decrypted Data:");
string t = Decipher(cipherText, key);
Console.WriteLine(t);
Console.Write("\n");
Console.ReadKey();
}
else;
{
Console.WriteLine("Invalid Input");
}
}
}
}
I have Simplified my code - so this must be abit simple to read
Have I integrated regex correctly
Wait... I have solved the issue
Thank you for your help so much i really appreciate it