C
C#2y ago
maroonlol

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
Angius
Angius2y ago
RegEx probably [a-zA-Z0-9] will cover all alphanumerical characters
maroonlol
maroonlol2y ago
Yes, but I would like only alphabetical characters not numberical
Angius
Angius2y ago
[a-zA-Z] then
maroonlol
maroonlol2y ago
And I have no idea how to implement it so can you show me a e example of you have time
maroonlol
maroonlol2y ago
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
Angius
Angius2y ago
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 somehow
maroonlol
maroonlol2y ago
Im still unsure how to add it in context with my code
maroonlol
maroonlol2y ago
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...
maroonlol
maroonlol2y ago
I looked at this website and implemented my code here
maroonlol
maroonlol2y ago
maroonlol
maroonlol2y ago
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; }
maroonlol
maroonlol2y ago
However I get a red error line under regex
maroonlol
maroonlol2y ago
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
mrphil2105
mrphil21052y ago
Why ASP.NET Core role?
maroonlol
maroonlol2y ago
I tried to tag the member my bad
Angius
Angius2y ago
You're missing the namespace import Use quick fixes to import it
maroonlol
maroonlol2y ago
That has helped got rid of the error
maroonlol
maroonlol2y ago
maroonlol
maroonlol2y ago
However it still has the same issue as before
Angius
Angius2y ago
Well, you defined a local function IsAlphabet But are you using it? I don't see you actually checking the input
maroonlol
maroonlol2y ago
static 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(); }
Angius
Angius2y ago
Cool cool
maroonlol
maroonlol2y ago
I have here
Angius
Angius2y ago
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
maroonlol
maroonlol2y ago
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
Angius
Angius2y ago
You never call the function So it will never run
maroonlol
maroonlol2y ago
How would I call the functipn
Angius
Angius2y ago
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
Angius
Angius2y ago
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 output
maroonlol
maroonlol2y ago
Do 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");
Angius
Angius2y ago
For example, yes
maroonlol
maroonlol2y ago
I have changed my code but it does the same
maroonlol
maroonlol2y ago
Angius
Angius2y ago
Angius
Angius2y ago
Remove the semicolon here
if (something);
{
Code here
}
if (something);
{
Code here
}
is equal in function to
if (something)
{

}
Code here
if (something)
{

}
Code here
That's why I recommended you learn the basics first
Trace
Trace2y ago
ping
maroonlol
maroonlol2y ago
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