473,503 Members | 3,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i align the input text to the left?

11 New Member
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?
Mar 20 '12 #1
12 5014
donbock
2,426 Recognized Expert Top Contributor
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.
Mar 20 '12 #2
logantr
11 New Member
Like at ms office..
and like this left tag.
Mar 20 '12 #3
donbock
2,426 Recognized Expert Top Contributor
I'm afraid I don't understand. Please post an example showing both the input text and the resultant left-aligned output text.
Mar 20 '12 #4
logantr
11 New Member
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.
Mar 20 '12 #5
donbock
2,426 Recognized Expert Top Contributor
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)?
Mar 20 '12 #6
logantr
11 New Member
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.
Mar 20 '12 #7
donbock
2,426 Recognized Expert Top Contributor
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.
Mar 20 '12 #8
logantr
11 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int main()
  5.  
  6. {
  7.     char original[500],output[61],word[61];
  8.  
  9.     cout << "Give someting: ";
  10.     cin >> original;
  11.  
  12.     int word_pos=0, original_pos=0;
  13.  
  14.     while(1)
  15.     {
  16.         while ((original[original_pos] == ' ') && (original[original_pos] == '\n') && (original[original_pos] == '\t') && (original[original_pos] == '\0'))
  17.         {
  18.             original_pos++;
  19.         }
  20.  
  21.         if (original[original_pos] == '\0')
  22.         {
  23.             cout << output << endl;
  24.             return 0;
  25.         }
  26.  
  27.         while ((original[original_pos] != ' ') && (original[original_pos] != '\n') && (original[original_pos] != '\t') && (original[original_pos] != '\0'))
  28.         {
  29.             word[word_pos] = original[original_pos];
  30.             word_pos++;
  31.             original_pos++;
  32.         }
  33.  
  34.  
  35.  
  36.         if ((word_pos + strlen(output)) > 60)
  37.         {
  38.             word[word_pos] = '\0';
  39.             cout << output << endl;
  40.             strcpy(output,word);
  41.             word_pos=0;
  42.         }
  43.         else
  44.         {
  45.             word[word_pos] = ' ';
  46.             word_pos++;
  47.             word[word_pos] = '\0';
  48.             strcat(output,word);
  49.             word_pos=0;
  50.         }
  51.         if (original[original_pos] == '\0')
  52.         {
  53.             //cout << output << endl;
  54.             return 0;
  55.         }
  56.     }
  57.     return 0;
  58. }
As we talk, I wrote this and couldn't find where is my mistake.
Mar 26 '12 #9
donbock
2,426 Recognized Expert Top Contributor
Please explain what is going wrong with that code. It helps to know the symptoms.
Mar 26 '12 #10
logantr
11 New Member
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..
Mar 26 '12 #11
swapnali143
34 New Member
you can use setw() method like this

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

}
Mar 29 '12 #12
logantr
11 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <conio.h>
  4. #include <ctype.h>
  5.  
  6. void alignLeft(char sentence[]);//function prototype
  7.  
  8. int main()
  9. {
  10.     char sentence[500];//takes until 500 character from user
  11.  
  12.     alignLeft(sentence);
  13.  
  14.     system("PAUSE");
  15.     return 0;
  16. }   
  17.  
  18.  
  19. void alignLeft(char sentence[]){//definition func.
  20.  
  21.     printf("Please input a sentence:\n");
  22.  
  23.     gets(sentence);
  24.     int word_pos=0;
  25.  
  26.     for(int i=1;sentence[i]!='\0';i++)//takes sentence until end of sentence: \0
  27.     {
  28.         if(isspace(sentence[i]))//if sentence is white space or \n or ' ' or\t... then word_pos equals i
  29.  
  30.             word_pos=i;
  31.  
  32.         if(i % 60 ==0)//writes each sentence 60 character 
  33.         {
  34.             if(isspace(sentence[i]))
  35.                 sentence[i]='\n';//after divide first 60 charakter with mod then write in newline with \n 
  36.             else
  37.                 sentence[word_pos]='\n';
  38.         }
  39.     }    
  40.     puts(sentence);    
  41. }
i did it with different way. Thanks for helping.
Mar 29 '12 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2710
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
1
1702
by: Raja Gopal | last post by:
Hi, Can anyone please tell me how do I align the text on a label or a link button so that it looks better. Thanks, Raja Gopal
3
4205
by: Ali | last post by:
I have 3 html input tex in my asp.net form. Two of them are calling javascript client side to calculate the differnce of two dates and put the result into the third input text. i haven't include...
2
2508
by: magix | last post by:
Hi, I'm using Access Database with ASP. There is one particular thing that I have issue with. My purpose is for user to update their own profile. Their existing profile information will be...
3
3965
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
3
2839
by: kvnsmnsn | last post by:
I've written the following Javascript file that includes an input text field and an output text field, the latter of which is initialized to zero. Each time the user enters a number in the input...
2
1894
idsanjeev
by: idsanjeev | last post by:
hello how can retrive the input text after submit button pressed and report a error message. i wants to post topic and if any error message is occured then retrive the inputed text but it forget its...
4
2247
by: backups2007 | last post by:
I want to be able to pass rows of queried data to rows of input text boxes. As the example below shows, I have come up with this incomplete solution. But this code only passes the data to the first...
2
9924
by: ismailc | last post by:
Hi, I need help please! I want to vertical align the text within a text box. I can set the vertical align of the text box but not the text. My text box has a style property of: style...
3
13262
by: happyse27 | last post by:
Hi All, I wanted to align the text box for user registration but the code just wont budge... Kindly advise what is wrong? Cheers... Andrew <HTML>
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.