K
K
KirillIvanov282015-10-18 21:44:54
C++ / C#
KirillIvanov28, 2015-10-18 21:44:54

I decided to write a library. There are no errors, but when testing, it says that the index is outside the array boundaries. How to fix it ???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlphaEditor
{ //весь код считает кол-во слов в тексте 
    public class EditText
    {
        public static int NumberOfWords(string text)
        {
            string znaki = @" [email protected]#$%^&*()_+-={}[]<>,./\1234567890";
            int lenght = text.Length;
            int i, j, n, result;
            char x;
            bool t = true;
            bool f = false;
            bool h;
            result = 0;
            for (i = 0; i == lenght; i++)
            {
                x = text[i];
                h = Helper(x,znaki,t);
                if (h)
                {
                    for (j = 1; j == lenght; j++)
                    {
                        n = i + j;
                        x = text[n];
                        h = Helper(x, znaki, f);
                        if(h)
                        {
                            result += 1;
                            i = n;
                            break;
                        }
                    }
                }
            }
            h = Helper(text[lenght], znaki, f);
            if (h)
            {
                result += 1;
            }
            h = Helper(text[0], znaki, t);
            if (h)
            {
                result -= 1;
            }
            return result;         
        }
        private static bool Helper(char a, string b, bool w)
        {
            int i=0;
            int c = b.Length;
            if (c == 0)
            {
                return (false);
            }
            if (i == c)
            {
                return false;
            }

            if (w == true)
            {


                for (i = 0; i == c; i++)
                {
                    if (a == b[i])
                    {
                        return (true);
                    }
                    else
                    {
                        return (false);
                    }
                }
            }
            else
            {
                for (i = 0; i == c; i++)
                {
                    if (a != b[i])
                    {
                        return (true);
                    }
                    else
                    {
                        return (false);
                    }
                }
            }
            return (false);
        }
    }     
}

2b8fdb8811cc4f3082c19fc4e8feea0a.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
AtomKrieg, 2015-10-19
@KirillIvanov28

You are rightly told that the whole code is one big mistake. If you did not take it from www.govnokod.ru , but wrote it yourself, then I have bad news for you.
Here is an example of an error:
If i = length, j = length, then n = 2*length and this is already more than the size of the array.

n = i + j;
x = text[n];

D
Dmitry Makarov, 2015-10-18
@DmitryITWorksMakarov

Is this some kind of joke? I have never seen more disgusting code in my life.
This is from the cycle:
- where is the mistake?
- in dna...

M
MrDywar Pichugin, 2015-10-18
@Dywar

write dll?
You are here Class Library Development Guide , Code Analysis Ruleset Reference .

S
sivabur, 2015-10-23
@sivabur

Words in the text are counted by this line
In general, you have written a very strong bicycle, start with easier tasks. Look at other people's code.
Cycle on sharpe is usually written in this format in the trailer as in other languages

for (int i = 0; i < c; i++)
{
...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question