Reputation: 19
I need some help creating a program that requires input with first letter in UPPER case and all other in lower case.
EDIT: I think some of u didnt understand the problem. I have to create a loop that requires from the user to enter the first number in upper and others in lower, if the requirements are not met, the user must input once again, until the first letter is upper and others are lower.
var novaDrzava = new Država ();
Console.Write ("Vnesite ime (prva začetnica naj bo velika, ostale male): ");
novaDrzava.Ime = Console.ReadLine ();
var drzava = novaDrzava.Ime;
var inicialka = drzava.Substring (0);
var ostale = drzava.Substring (1, drzava.Length - 1);
for (int i = 0; i <= malecrke.Length; i++) {
if (inicialka.Contains (velikecrke[i])) {
if (ostale.Contains (malecrke[i])) {
break;
} else {
Console.WriteLine ("Ponovno vnesite ime");
novaDrzava.Ime = Console.ReadLine ();
}
}
}
Upvotes: 1
Views: 1426
Reputation:
private static string CapitaliseFirstLetter(string str)
{
var Ustr = string.Empty;
if (!String.IsNullOrEmpty(str))
{
Ustr = char.ToUpper(str.First()) + str.Substring(1).ToLower();
}
return Ustr;
}
Upvotes: 0
Reputation: 21
static void Main(string[] args)
{
string inputValue = Console.ReadLine();
bool isValid = true;
foreach (char val in inputValue)
{
if (inputValue.First()==val && char.IsUpper(val))
{
//do nothing.
}
else if(char.IsLower(val))
{
// do nothing.
}
else
{
isValid = false;
Console.WriteLine("Invalid input string");
Console.ReadLine();
break;
}
}
if (isValid)
{
Console.WriteLine("Valid input string");
Console.ReadLine();
}
}
Upvotes: 1
Reputation: 186718
You are, probably, looking for Title Case where each word starts from capital letter (e.g. John Smith
). If it's your case:
// Normilize: turn modificators in diactritics (e.g. "Hašek")
string drzava = Console.ReadLine().Normalize();
if (string.Equals(drzava, CultureInfo.CurrentCulture.TextInfo.ToTitleCase(drzava))) {
// Correct name in title case
}
Or if we want just single name (e.g. John
, but not John Smith
)
if (!string.IsNullOrEmpty(drzava) &&
drzava.All(c => char.IsLetter(c)) &&
string.Equals(drzava, CultureInfo.CurrentCulture.TextInfo.ToTitleCase(drzava))) {
// Correct Name : Not empty, Letters only, Title Case
}
Finally, you can try regular expressions
using System.Text.RegularExpressions;
...
//TODO: change "*" into "+" if you want at least one lowercase symbol
if (Regex.IsMatch(drzava, @"^\p{Lu}\p{Ll}*$")) {
// Correct Name : Starts from upper case contains zero or more lowercase
}
Upvotes: 0
Reputation: 1073
If I understand it correctly it should be not difficult: just take the first letter with
var firstletter = yourstring.Substring(0, 1);
everything else with
var everthingelse = yourstring.Substring(1);
firstletter = firstletter.ToUpper();
everthingelse = everthingelse.ToLower();
Upvotes: 1
Reputation: 21
You might want to look into regular expressions. Something like this:
string inputOk = "Thisisatest";
string inputNok1 = "ThisisaTest";
string inputNok2 = "thisisatest";
bool resultOk = Regex.IsMatch(inputOk, "^[A-Z]{1}[a-z]+$");
bool resultNok1 = Regex.IsMatch(inputNok1, "^[A-Z]{1}[a-z]+$");
bool resultNok2 = Regex.IsMatch(inputNok2, "^[A-Z]{1}[a-z]+$");`
Upvotes: 1