How can i align the input text to the left?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • logantr
    New Member
    • Mar 2012
    • 11

    How can i align the input text to the left?

    i want to write a program that input a text from user and align it to the left. Is there any command that i can use? How should i think?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What do you mean by "align it to the left"?
    • Remove leading whitespace.
    • Print out a left-aligned copy of the input text.
    • Store a left-aligned copy of the input text in a character array. In a string. In a vector.
    • Something else.

    Comment

    • logantr
      New Member
      • Mar 2012
      • 11

      #3
      Like at ms office..
      and like this left tag.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        I'm afraid I don't understand. Please post an example showing both the input text and the resultant left-aligned output text.

        Comment

        • logantr
          New Member
          • Mar 2012
          • 11

          #5
          I couldn't tell. If a word at the end of line exceeds the column, shift it to the next line without dividing it. And every column should be 60 characters.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            I would have called this "wrap text" rather than "align to the left".

            Does this sound like what you want to do?
            1. Obtain line of text from the user.
            2. Strip trailing whitespace from the text.
            3. Repeat:
            4. ... Split the text at the beginning of the last whitespace sequence before the 60th character.
            5. ... Output the text preceding the split.
            6. ... Strip leading whitespace from text following the split.
            7. ... Text = the text following the split.
            8. Until (all text has been outputed).


            Even if that sounds right, you still need to make some decisions:
            • How do you want to handle newlines or carraige returns embbed within the text?
            • How do you want to handle tab characters embedded within the text?
            • Do you want to handle whitespace at line breaks the way I suggest above (that is, throw it away)?

            Comment

            • logantr
              New Member
              • Mar 2012
              • 11

              #7
              I understood what you mean and i will write it, except '... Split the text at the beginning of the last whitespace sequence before the 60th character.' i should try, because i ve some problems in this algoritms. You gave me an general idea that i need(want). Thanks donbock.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Another special case you need to think about: what do you want to do when there is a sequence of more than 60 non-whitespace characters? (That is, when it is impossible to break the line at a word boundary because you have a single word that is longer than the line limit.)

                The logic I suggested earlier assumed that you want to preserve all leading whitespace. I should instead have invited you to decide what you want to do.

                Comment

                • logantr
                  New Member
                  • Mar 2012
                  • 11

                  #9
                  Code:
                  #include <iostream>
                  #include <cstring>
                  using namespace std;
                  int main()
                  
                  {
                      char original[500],output[61],word[61];
                      
                      cout << "Give someting: ";
                      cin >> original;
                      
                      int word_pos=0, original_pos=0;
                      
                      while(1)
                      {
                          while ((original[original_pos] == ' ') && (original[original_pos] == '\n') && (original[original_pos] == '\t') && (original[original_pos] == '\0'))
                          {
                              original_pos++;
                          }
                          
                          if (original[original_pos] == '\0')
                          {
                              cout << output << endl;
                              return 0;
                          }
                          
                          while ((original[original_pos] != ' ') && (original[original_pos] != '\n') && (original[original_pos] != '\t') && (original[original_pos] != '\0'))
                          {
                              word[word_pos] = original[original_pos];
                              word_pos++;
                              original_pos++;
                          }
                          
                          
                          
                          if ((word_pos + strlen(output)) > 60)
                          {
                              word[word_pos] = '\0';
                              cout << output << endl;
                              strcpy(output,word);
                              word_pos=0;
                          }
                          else
                          {
                              word[word_pos] = ' ';
                              word_pos++;
                              word[word_pos] = '\0';
                              strcat(output,word);
                              word_pos=0;
                          }
                          if (original[original_pos] == '\0')
                          {
                              //cout << output << endl;
                              return 0;
                          }
                      }
                      return 0;
                  }
                  As we talk, I wrote this and couldn't find where is my mistake.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Please explain what is going wrong with that code. It helps to know the symptoms.

                    Comment

                    • logantr
                      New Member
                      • Mar 2012
                      • 11

                      #11
                      It takes the string value but it is not give any kind of response then close itself. I can't find where is the problem..

                      Comment

                      • swapnali143
                        New Member
                        • Mar 2012
                        • 34

                        #12
                        you can use setw() method like this

                        #include<iostre am.h>
                        void main()
                        {
                        cout<<setw(20)< <"Swapnali"; //set width =20
                        getch();

                        }

                        Comment

                        • logantr
                          New Member
                          • Mar 2012
                          • 11

                          #13
                          Code:
                          #include <iostream>
                          #include <cstring>
                          #include <conio.h>
                          #include <ctype.h>
                          
                          void alignLeft(char sentence[]);//function prototype
                          
                          int main()
                          {
                          	char sentence[500];//takes until 500 character from user
                          	
                          	alignLeft(sentence);
                          
                          	system("PAUSE");
                          	return 0;
                          }   
                          
                          
                          void alignLeft(char sentence[]){//definition func.
                              
                              printf("Please input a sentence:\n");
                              
                          	gets(sentence);
                              int word_pos=0;
                          
                          	for(int i=1;sentence[i]!='\0';i++)//takes sentence until end of sentence: \0
                          	{
                          		if(isspace(sentence[i]))//if sentence is white space or \n or ' ' or\t... then word_pos equals i
                          		
                          			word_pos=i;
                          		
                          		if(i % 60 ==0)//writes each sentence 60 character 
                          		{
                          			if(isspace(sentence[i]))
                          				sentence[i]='\n';//after divide first 60 charakter with mod then write in newline with \n 
                          			else
                          				sentence[word_pos]='\n';
                          		}
                          	}	
                          	puts(sentence);	
                          }
                          i did it with different way. Thanks for helping.

                          Comment

                          Working...