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?
How can i align the input text to the left?
Collapse
X
-
I would have called this "wrap text" rather than "align to the left".
Does this sound like what you want to do?- Obtain line of text from the user.
- Strip trailing whitespace from the text.
- Repeat:
- ... Split the text at the beginning of the last whitespace sequence before the 60th character.
- ... Output the text preceding the split.
- ... Strip leading whitespace from text following the split.
- ... Text = the text following the split.
- 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
-
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
-
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
-
As we talk, I wrote this and couldn't find where is my mistake.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; }Comment
-
you can use setw() method like this
#include<iostre am.h>
void main()
{
cout<<setw(20)< <"Swapnali"; //set width =20
getch();
}Comment
-
i did it with different way. Thanks for helping.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); }Comment
Comment