Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string input="Flat No-4324,Near Ganga Institute, Tehsil-Raipur, Distt-Banglore,State-Karnataka Pin-644487 India";

in the input string perhaps there are 96 character.

i want to make a program by which input.length always divided by 20(input.length/20)
and print,so in this case input will print in 5 line.In first line the last word is -Ganga(here in first line it will print-Flat No-4324,Near Ga)and the remaining again in 2nd line starting with- nga Institute.......(so here is problem)

i want that if the last letter length exceeded from length 20 then it should print from 2nd line,as i want then 1st line will be(Flat No-4324,Near ) then 2nd line start with Ganga Institute..... and if again this condition occur then so on .


If u r not getting so let's take below explanation--

i m saying that if,my every line length will be suppose 11 character,so there are some problem occur to print strings like this->( I want to play football and I want to play chess.)
1st line will take 11 character which will be(I want to p),in 2nd line will print(lay footbal) and so on.So here u can see play divide into 2 words p and lay,again football is dividing in 2 words footbal and l,so this is a problem.I want if this type of situation occur then complete word should be start from next line,as i want then 1st line will be(I want to) then 2nd line will be(play) and in 3rd line (football) and so on.

plz help
Posted
Updated 22-Oct-12 5:46am
v2
Comments
Akkywadhwa 22-Oct-12 8:52am    
Can you please show any example of line or of the thing u r saying about.
Akky
StackQ 22-Oct-12 11:37am    
i m saying that if,my every line length will be suppose 11 character,so there are some problem occur to print-( I want to play football and I want to play chess.)
1st line will take 11 character which will be(I want to p),in 2nd line will print(lay footbal) and so on.So here u can see play divide into 2 words p and lay,again football is dividing in 2 words footbal and l,so this is a problem.I want if this type of situation occur then complete word should be start from next line,as i want then 1st line will be(I want to) then 2nd line will be(play) and in 3rd line (football) and so on.
Richard MacCutchan 22-Oct-12 14:33pm    
It is never a good idea to parse sentences based on numbers of characters. I think you should still follow my suggestion of using String.Split(). You then need to test the different parts of the text for keywords to decide where to leave the breaks and where to rejoin some words.

Why not just use String.Split() and break it at the commas? That will have the double benefit of separating the elements of the address correctly, and working for any length and number of items.
 
Share this answer
 
Comments
StackQ 23-Oct-12 0:58am    
this one is best suit for me.Thnk u so much
Try this:

C#
namespace myTest
{
    class Program
    {
        static void Main()
        {
            string _Input = "I want to play football and I want to play chess";
            string _ModifiedInputWithLineFeeds = StringToDefiniteLengthLines(_Input, 11);
        }

        static string StringToDefiniteLengthLines(string input, int length)
        {
            string[] words = System.Text.RegularExpressions.Regex.Split(input, "[^\\w]");
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string NewLineCost = Environment.NewLine;
            foreach (string word in words)
            {
                string temp = sb.ToString();

                int wordLength = word.Length + 1;
                int tempLength = temp.Length;
                int totalLength = wordLength + tempLength;

                int lastindex = temp.LastIndexOf(NewLineCost);
                int intermediateLength = 0;
                if (lastindex > -1)
                    intermediateLength = wordLength + temp.Substring(lastindex).Length;

                if (intermediateLength > 0)
                    totalLength = wordLength + intermediateLength;
                if (totalLength < length)
                    sb.Append(word+" ");
                else
                    sb.AppendLine(word);
            }
            return sb.ToString();
        }
    }
}

Output:
I want to play
football
and I want
to play
chess
 
Share this answer
 
Comments
StackQ 23-Oct-12 2:02am    
perfect answer.But one problem in case suppose input is address-My House no-01, distt-Newyork, pin-896547, India (so in this case the program removes(',' and '-' character),is there any solution to print , and - value.

but thnx for helping.....
I will suggest you to change your logic .
instead of spliting string on the index split on the where you have WhiteSpace.

C#
string input = "Flat No-4324,Near Ganga Institute, Tehsil-Raipur, Distt-Banglore,State-Karnataka, Pin-644487, India";
            string[] Words = input.Split(',');
            char[] L1,L2,L3,L4,L5 = new char[20];
            L1[0] = L2[0] = L3[0] = L4[0] = L5[0] = ' ';
            bool P1,  P2,  P3,  P4, P5;
            P1 = P2 = P3 = P4 =P5 = true;
            int spaceleft = 0;

            foreach (string s in Words)
            {
                if (P1)
                {
                    spaceleft = 20 -L1.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L1.Length+1;i<20;i++)
                      {
                          
                          L1[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P1 = false;
                        foreach (char c in L1)
                            Console.Write(c);
                        Console.WriteLine();
                        
                    }
                }
                else if (P2)
                {
                     spaceleft = 20 -L2.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L2.Length+1;i<20;i++)
                      {
                          
                          L2[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P2 = false;
                        foreach (char c in L2)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P3)
                {
                     spaceleft = 20 -L3.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L3.Length+1;i<20;i++)
                      {
                          
                          L3[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P3 = false;
                        foreach (char c in L3)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P4)
                {
                     spaceleft = 20 -L4.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L4.Length+1;i<20;i++)
                      {
                          
                          L4[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P4 = false;
                        foreach (char c in L4)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P5)
                {
                    spaceleft = 20 - L5.Length;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar = s.ToCharArray();
                        for (int i = L5.Length + 1; i < 20; i++)
                        {

                            L5[i] = wordchar[j];
                        }
                    }
                    else
                    {
                        P5 = false;
                        foreach (char c in L5)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("Address too long.");
                }

 
            }


I wrote this sample code in very pathatic manner, so you have to refactor it and then use it. there may be some execptions too
 
Share this answer
 
Comments
Richard MacCutchan 22-Oct-12 11:31am    
Why do you make this so complicated, the answer takes about 5 lines of code at most.
Sushil Mate 22-Oct-12 11:57am    
whats the need of all this?
[no name] 22-Oct-12 13:14pm    
Apologies guys !! :) ... i have mentioned that i wrote a PATHETIC code.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RenfroAddress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int forward,charperline;  //variable for going forward and take input char per line
        string print,nextlineString,previouString = null;
        //variable print=>it stores words according to count(based on for loop) per string after splitting (from array)
        //nextlinestring=>it takes next sentence remaining after printing line by line and so on
        //previoustring=>stores the sentence which we have to print after going to nextlinstring(previous line string for print)
        private void button1_Click(object sender, EventArgs e)
        {
            charperline = Convert.ToUInt16(textBox1.Text);
            //string input = "Flat No-4324, Near Ganga Institute, Tehsil-Raipur, Distt-Banglore, State-Karnataka, Pin-644487;
            string str = textBox2.Text;   //will take input (as address from textbox2)
            string[] strArr = new string[charperline];
            int count = 0;
            char[] splitchar = { ' ' };
            strArr = str.Split(splitchar);
            for (count = 0;count <= strArr.Length - 1; count++)
            {                
                int strLength = strArr[count].Length; //getting length
                forward += strLength; //for pointing next words to words
                if (forward > charperline)
                {
                    int indx = str.IndexOf(strArr[count].ToString());
                    nextlineString = str.Substring(indx);
                    richTextBox1.Text += previouString + Environment.NewLine;
                    previouString = null;
                    print = null;
                    strArr = null;
                    strArr = nextlineString.Split(splitchar);
                    count = 0;
                    count--;
                    forward = 0;
                }
                else
                {
                    print = strArr[count];
                    previouString += print + " ";
                }

            }
            richTextBox1.Text += previouString; //if suppose last line is less than charperline then it not print above so i wrote here this for last sentence

        }

    }
}
 
Share this answer
 
v2

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