Thread: Justifying text with strings

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    10

    Justifying text with strings

    Code:
    #include<stdio.h>
    #include<string.h>
    char line[100];
    int i, n, len, width, gaps, spacestoshare, equalshare, extras;
    int main() {
    	gaps=0;
    	printf("Please enter the width of the column:");
    	scanf("%i", &width);
    	fflush (stdin);
    	printf("\nPlease enter a line of text");
    	gets (line);
    	len = strlen (line);
    	while (len>width) {
    		printf("\nError, your line of text is too long, please re-enter a");
    		printf(" shorter line of text:");
    		gets (line);
    		len = strlen (line);
    	}
    	for (i=0; i<=width; i++) {
    		if (line[i]==' ') {
    			gaps++;
    		}
    	}
    	spacestoshare = width-len;
    	equalshare = spacestoshare/gaps;
    	extras = spacestoshare%gaps;
    	for (i=0; i<=width; i++) {
    		printf("%c", line[i]);
    		if (line[i]==' ') {
    			for (n=0;n<=equalshare;n++) {
    				printf(" ");
    			}
    		 	if (extras!=0) {
    			 	printf(" ");
    			 	extras-=1;
    			}
    		}
    	}
    }
    im trying to run this program, it works, but i need the cursor to be directly after the last letter of text, and when i run it it ends up being about 7 spaces after the last letter. any help is greatly appreciated
    Last edited by Salem; 04-24-2004 at 12:34 AM. Reason: Added code tags

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    first use code tags. Dont' use gets, don't use fflush(stdin) see the FAQ for answers to why. If you would indent your code you would realize you are short on } braces. See if you can change those first. for gets try fgets, and for fflush(stdin); use while((c=getchar()!='\n') && (c=getchar()!=EOF)); Also dont' forget to indent

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    like i sed, the program runs and i just need to fix the spaces problem, we have been told to use fflush so i will thank you very much

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    try using these types of flags then
    Code:
    %4i
    %3.2f
    %3.0f
    %5d
    those move your variables around.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by linuxdude
    use while((c=getchar()!='\n') && (c=getchar()!=EOF))
    You do realise that this will actually read two characters here, and would then fail on if you had something like" \n" (space, enter) in your buffer, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > we have been told to use fflush
    So now you know not to trust everything your teacher tells you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but i need the cursor to be directly after the last letter of text
    Are you left justifying or right justifying? Your code says left, and that means that the spaces will be on the right side of the output. If you want to right justify then you'll have spaces on the left side of every word, which requires a slightly different algorithm:
    Code:
    /* Bad code. Use at your own peril */
    #include<stdio.h>
    #include<string.h>
    char line[100];
    char *t;
    int i, n, len, width, gaps, spacestoshare, equalshare, extras;
    int main() {
      gaps=0;
      printf("Please enter the width of the column:");
      scanf("%i", &width);
      fflush (stdin);
      printf("\nPlease enter a line of text");
      gets (line);
      len = strlen (line);
      while (len>width) {
        printf("\nError, your line of text is too long, please re-enter a");
        printf(" shorter line of text:");
        gets (line);
        len = strlen (line);
      }
      for (i=0; i<width; i++) {
        if (line[i]==' ') {
          gaps++;
        }
      }
      spacestoshare = width-len;
      if (++gaps == 0) {
        equalshare = spacestoshare;
        extras = 0;
      }
      else {
        equalshare = spacestoshare/gaps;
        extras = spacestoshare%gaps;
      }
      for (t=strtok (line, " "); t!=NULL; t=strtok (NULL, " ")) {
        for (n=0;n<equalshare;n++) {
          printf(" ");
        }
        if (extras!=0) {
          printf(" ");
          extras-=1;
        }
        printf ("%s", t);
      }
      printf("\n");
    
      return 0;
    }
    >we have been told to use fflush so i will thank you very much
    You should be more selective in who you follow orders from. Your teacher is stupid, and we will continue to nag you about it when you post code like that.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Multiple Multi-Part Strings From a Text File
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 10:43 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM