1

I need some help creating a program that requires input with first letter in UPPER case and all other in lower case.

I tried to write some code, but I can't seem to figure it out.

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 ();
        }
    }
}
matej
  • 19
  • 5
  • Why not check the first letter against a regex containing only upper case chars and the remainers only lower case? – Markus Deibel Mar 29 '19 at 11:48
  • 2
    Possible duplicate of [Make first letter of a string upper case (with maximum performance)](https://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case-with-maximum-performance) – Martin M Mar 29 '19 at 11:50
  • its really hard to read if the variables are cryptically named... – slow Mar 29 '19 at 11:50
  • To clarify - what do you mean by "requires?" Do you want a function that takes a string and returns true/false to indicate whether it meets the requirements? If so, which of these would pass and which would fail: Test TEst 7Test Test7 – Scott Hannen Mar 29 '19 at 11:54
  • Since you are getting input from your user, then it is possible that you get a mixed lower/upper letters in your input. Like _"this IS a MixeD case tEXt"_ – Steve Mar 29 '19 at 12:28
  • @ScottHannen there must be a loop that tells the user to enter the input so many times, until first letter is upper and others are lower. For example (John). – matej Mar 29 '19 at 13:53

5 Answers5

1

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();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
LiefLayer
  • 977
  • 1
  • 12
  • 29
  • 2
    `yourstring.Substring(1, yourstring.Length-1)` can be shortened to `yourstring.Substring(1)` – slow Mar 29 '19 at 11:53
  • you are right, I always use the long version because I never remember how the short version works. I will update my answer – LiefLayer Mar 29 '19 at 11:54
  • You'll have an *exception thrown* on *empty* `firstletter` string. – Dmitry Bychenko Mar 29 '19 at 12:11
  • 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. – matej Mar 29 '19 at 13:50
  • @DmitryBychenko "firstletter" cannot be empty if "yourstring" is not empty. Of course the user can check if(string.IsNullOrEmpty(yourstring)) before anything else. – LiefLayer Mar 29 '19 at 16:40
  • @matej Ok now I understand your question. I did understand that you wanted to force the user input to Upper case for the first letter and to Lower case for the rest of the string. Anyway, the loop part is different if you just want to create a command line application or a web/desktop application. For the check you should check KALINK answer – LiefLayer Mar 29 '19 at 16:44
1

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]+$");`
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
KALINK
  • 11
  • 3
  • `{1}` is redundant: `[A-Z]` and `[A-Z]{1}` are the same ; `[A-Z]` and `[a-z]` is *not enough*: `Hašek` is a correct name – Dmitry Bychenko Mar 29 '19 at 12:07
  • Be careful with regular expressions: it seems we have to work with *Czech* culture – Dmitry Bychenko Mar 29 '19 at 12:15
  • I did see that there were some diacritics in the code but the question wasn't specified in that detail hence "Something like this". If you want to normalize your text before comparing one could do something like this: public string RemoveDiacritics(string text, Encoding encoding) { if(string.IsNullOrEmpty(text) == true) { return string.Empty; } return Encoding.ASCII.GetString(encoding.GetBytes(text)); } – KALINK Mar 29 '19 at 14:07
1
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();
            }

        }
arun vats
  • 21
  • 1
0

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
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
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;
    }
  • 1
    Add test on string.isNullOrEmpty. Also your response is incomplete, you need to lower the following char of the string. – LeBigCat Mar 29 '19 at 15:37
  • Please describe, what did you change and why, to help others understand the problem and this answer – FZs Mar 29 '19 at 19:35