none
Counting the same groups of sighns in text RRS feed

  • Question

  • Hello 

    Was writing a program that should count groups of sighns but sadly code is not working and I cannot understand the problem.  

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void NactiText(string text)
            {
                Console.WriteLine("Input text:");
                do
                {
                    text = Console.ReadLine();
                    if (text == "") Console.WriteLine("Text shoudnt be empty.");
                } while (text != "");
            }
    
            static bool ThisIsLetter(char sign)
            {
                if ((sign >= 'a' && sign <= 'z') && (sign >= 'A' && sign <= 'Z')) return true;
                else return false;
            }
    
            static void Analize(string text)
            {
                int[] array = new int[1000];
                for (int i = 0; i < 1000; i++) array[i] = 10;
                char sign = ' ';
                int count = 0;
                text = text + " ";
                Console.WriteLine("Analizing text: " + text);
                for (int i = 0; i <= text.Length; i++)
                {
                    if (text[i] == sign && ThisIsLetter(text[i]))
                    {
                        count--;
                    }
                    else
                    {
                        if (count >= 2 && count < 1000) array[count]++;
                        count = 1;
                    }
                    sign = text[i];
                }
                Console.WriteLine("Groups:");
                for (int i = 2; i < 1000; i++)
                {
                    if (array[i] == 0) Console.WriteLine("Lenght " + i + " : " + array[i] + " times.");
                }
                Console.WriteLine("Press any key to go back.");
                Console.ReadKey();
            }
    
    
    
            static void Main(string[] args)
            {
                char volba = '9';
                string text = "";
                while (volba != '9')
                {
                    Console.Clear();
                    Console.WriteLine("Program is counting groups of signs");
                    Console.WriteLine("For example text: \"AAaa1111bbbc\" has two groups of lenght 2 and one group of lenght 3.");
                    Console.WriteLine("Choose an option:");
                    Console.WriteLine("1 - Input text");
                    Console.WriteLine("2 - Analize text");
                    Console.WriteLine("----------------");
                    Console.WriteLine("9 - Konec");
                    volba = Console.ReadKey().KeyChar;
                    Console.Clear();
                    if (volba == '1') NactiText(text);
                    if (volba == '2') Analize(text);
                }
            }
        }
    }

    Monday, June 4, 2018 4:05 PM

All replies

  • Hello there!

    Just press F10 several times - you will see a first trouble there:

                while (volba != '9')

    Don't give up :)

    Monday, June 4, 2018 4:30 PM
  • It goes char by char and count same chars. When next char is different than previous it increases counter (array) by length which is created by same chars. But only if count of next same chars is between 2 and 1000. I think count-- is wrong.
    Monday, June 4, 2018 4:34 PM
  • Hi Useeeer,

    I test the code. In the Main method, volba!='9' will always be false. It could not do the while loop. The condition is meaningless.

    Please check your code. And if you need further help, please provide more details about what you want.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 6, 2018 1:39 AM
    Moderator