Jump to content

Afisarea caracterelor c#


Recommended Posts

Buna am un problema pe care nu stiu cum s-o rezolva

Trebuie sa fac un program care sa afiseze prima ultima si cea din mijloc litera a unui sir de caractere
 

{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Introduceti sirul de caractere"); 
            string s1 = Console.ReadLine(); 

            Console.WriteLine("\nPrima litera :");
            Console.WriteLine(s1[0]);
           
           Console.WriteLine("\nLitera din mijloc : ");

      
            Console.WriteLine("\nUltima litera :");
            Console.WriteLine(s1[s1.Length - 1]);
            
           
        }
    }
}

Prima si ultima am reusit sa aflu dar pe cea din mijloc nu stiu cum.
Ma puteti ajuta va rog?

Link to post
Share on other sites

  • Moderators

Pai te folosesti de caracterul din mijloc impartind lungimea sirului la 2. Poti lua cateva cazuri concrete sa vezi ce si cum. Dar aici depinde cum vrei sa tratezi sirurile care au numar par de caractere.

Daca are un numar impar de caractere, gen abcde si vrei sa afisezi c, lungimea e 5, se afla pe pozitia 2, deci e suficient sa afisezi sir[sir.Length / 2]

La numar par de caractere, metoda gen sirul abcd, metoda de mai sus iti fa afisa caracterul c. Aici depinde daca vrei sa afisezi b sau c. (sau ambele :) ..  sau niciunul :)))

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.