Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm trying to make this function return a neat table out of a string.

This is what I expect to get.

3.
1415926535 8979323846 2643383279 5028841971 6939937510
5820974944 5923078164 0628620899 8628034825 3421170679
8214808651 3282306647 0938446095 5058223172 5359408128
4811174502 8410270193 8521105559 6446229489 5493038196
4428810975 6659334461 2847564823 3786783165 2712019091
4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436
7892590360 0113305305 4882046652 1384146951 9415116094
3305727036 5759591953 0921861173 8193261179 3105118548
0744623799 6274956735 1885752724 8912279381 8301194912
9833673362 4406566430 8602139494 6395224737 1907021798
6094370277 0539217176 2931767523 8467481846 7669405132
0005681271 4526356082 7785771342 7577896091 7363717872
1468440901 2249534301 4654958537 1050792279 6892589235
4201995611 2129021960 8640344181 5981362977 4771309960
5187072113 4999999837 2978049951 0597317328 1609631859
5024459455 3469083026 4252230825 3344685035 2619311881
7101000313 7838752886 5875332083 8142061717 7669147303
5982534904 2875546873 1159562863 8823537875 9375195778
1857780532 1712268066 1300192787 6611195909 2164201989


However, this is what I actually get.

3.
1415926 535897932 384626433 832795028 841971693
9937510 582097494 459230781 640628620 899862803
4825342 117067982 148086513 282306647 093844609
5505822 317253594 081284811 174502841 027019385
2110555 964462294 895493038 196442881 097566593
3446128 475648233 786783165 271201909 145648566
9234603 486104543 266482133 936072602 491412737
2458700 660631558 817488152 092096282 925409171
5364367 892590360 011330530 548820466 521384146
9519415 116094330 572703657 595919530 921861173
8193261 179310511 854807446 237996274 956735188
5752724 891227938 183011949 129833673 362440656
6430860 213949463 952247371 907021798 609437027
7053921 717629317 675238467 481846766 940513200
0568127 145263560 827785771 342757789 609173637
1787214 684409012 249534301 465495853 710507922
7968925 892354201 995611212 902196086 403441815
9813629 774771309 960518707 211349999 998372978
0499510 597317328 160963185 950244594 553469083
0264252 230825334 468503526 193118817 101000313
7838752 886587533 208381420 617177669 147303598
2534904 287554687 311595628 638823537 875937519
5778185 778053217 122680661 300192787 661119590
9216420 1989


So, my question is why is this happening? and how could I possibly fix it?

Code:
C#
public string PrintAsTables()
{
    string reg = this.Print();
    reg = reg.Insert(2, Environment.NewLine);
    int times = 0;
    for (int x = 2; x <= reg.Length - 1; x = x + 10)
    {
        if (times == 5)
        {
            reg = reg.Insert(x, Environment.NewLine);
            times = 0;
        }
        times += 1;
         reg = reg.Insert(x, " ");

    }
    return reg;
}
Posted
Updated 21-Nov-13 5:01am
v2

C#
public static string PrintAsTables()
{
    string reg = Pi;
    reg = reg.Insert(2, Environment.NewLine);
    int times = 0;
    //14 because u had newLine so first space can be on 14th places
    for (int x = 14; x <= reg.Length - 1; x = x + 10)
    {
        times += 1;
        if (times == 5)
        {
            reg = reg.Insert(x, Environment.NewLine);
            x = x + 2;
            times = 0;
        }
        else
        {
            reg = reg.Insert(x, " ");
        }
    }
    return reg;
}


It's all about Environment.NewLine its definied by Environment but in windows is 2 special chars "\r\n" so if u inserting into string Environment.NewLine next index to display should be incremented too.
 
Share this answer
 
v2
Comments
OriginalGriff 21-Nov-13 11:43am    
It's not a good idea to do that - strings are immutable, remember, so every time you do an Insert operation, you are creating a new string and copying the old one over - much better to use a StringBuilder which is not immutable.
Piotr Bagazja 21-Nov-13 15:47pm    
Thx, I just "repaired" his model. I rather prefer to help somebody in his style without change model of thinking or introducing somethink new. I propably would be use similiar construction to yours
I would probably do it by using a StringBuilder to start with, and a couple of simple counters:
C#
string inp = "1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989";
StringBuilder sb = new StringBuilder();
int groupSize = 10;
int groupsPerLine = 5;
int inGroup = 0;
int inLine = 0;
foreach (char c in inp)
    {
    sb.Append(c);
    inGroup++;
    if (inGroup == groupSize)
        {
        sb.Append(' ');
        inGroup = 0;
        inLine++;
        if (inLine == groupsPerLine)
            {
            sb.AppendLine();
            inLine = 0;
            }
        }
    }
return sb.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900