-4

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();
        }
    }
}
Jordec
  • 1,516
  • 2
  • 22
  • 43
matej
  • 19
  • 5

3 Answers3

0

Keep looping until novaDrzava.Ime is valid:

  using System.Text.RegularExpressions;

  ...

  Console.Write("Vnesite ime (prva začetnica naj bo velika, ostale male): ");

  // Keep looping
  while (true) {
    // Get a new user input
    //   .Normalize() - let diacritics be represent in a standard way
    //   .Trim()      - let be nice and remove leading/trailinf white spaces 
    novaDrzava.Ime = Console.ReadLine().Normalize().Trim();

    // If input is valid one (uppercase followed zero or more lowercase) 
    // we can stop looping 
    if (Regex.IsMatch(novaDrzava.Ime, @"^\p{Lu}\p{Ll}*$")
      break;

    // input is not valid: let user know it and keep asking
    Console.WriteLine("Ponovno vnesite ime");
  }

  // From now on novaDrzava.Ime contains a valid name  
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Why not accept their input and format it the way you need? This would remove the need for the loop. Something like:

var teststring = ToUpperFirst("touppercase");

private string ToUpperFirst(string text)
 {
     return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());

     //alternatively, you could use:
     //return $"{char.ToUpper(text[0])}{(text.Length > 1 ? text.Substring(1).ToLower() : string.Empty)}";
 }
-3

A simple getline like there are in C & C++ should be enough, just use ifs inside to check data he already put in, and new data if they meet requirements go.

Else, if he put a bad data at a key moment ask him again and reset data he already put in.

This is a sample working in C so you should be able to replicate it in C# with a bit of rework, for one character input at one time.

#include <stdio.h>
#include <stdlib.h>

int is_upper(char *str)
{
    int idx = 0;

  if (str[idx] >= 'a' && str[idx] <= 'z') {
    return (0);
  }
  if (str[idx] >= 'A' && str[idx] <= 'Z') {
    return (1);
  }
  return (2);
}

int main(void)
{
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;

  int mystrlen = 0;
  char finalstring[2048]; //to ease the algorithm so there is no need to do a dynamic allocation due to C behavior

  while ((read = getline(&line, &len, stdin)) != -1) {
    if (strlen(line) == 2) { // because \n == 1 char
        if (mystrlen == 0 && line[0] >= 'A' && line[0] <= 'Z') {
            strcat(finalstring, line);
            mystrlen++; //first letter = UPPER CASE
        }
        else if (is_upper(line) == 2 || is_upper(line) == 1 || mystrlen == 0) { //if first letter is good & we aren't at first letter, or simply bad char input
            mystrlen = 0;
            finalstring[0] = '\0';
            printf("reset\n");
        }
        else if (is_upper(line) == 0) { //when all is good
            strcat(finalstring, line);
            printf("\n%s\n", finalstring);
        }
    }
    else {
        mystrlen = 0;
        finalstring[0] = '\0';
        printf("reset\n");
        }
    }
}
Andreas
  • 2,455
  • 10
  • 21
  • 24
dietis
  • 27
  • 3