Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 9:15 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: String Formatter
PostPosted: Thu Nov 13, 2008 3:23 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
/*
* Formats the input text into 75 character wide lines.  Takes each
* non-blank unit as a word, and fits as many as possible on each
* line.  It separates words by one space, unless the word ends in
* a period, !, or ?, and the next word starts with a capitol,
* in which case it gets two spaces.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define WIDTH           75              /* Width of a line. */
#define WORDSIZE        50              /* Storage size of input word. */

main()
{
        char line[WIDTH+1];             /* Output line. */
        char word[WORDSIZE];            /* Input word. */
        int linesize;                   /* Accumulated line size. */
        int wordsize;                   /* Size of inbound word. */
        int numspace;                   /* Number of separating spaces. */

        /* Initialize the output line. */
        line[0] = 0;
        linesize = 0;

        /* Read all the words in the input file. */
        while(scanf("%s", word) == 1)
        {
                /* How long is it? */
                wordsize = strlen(word);

                /* Decide how many spaces need to after the word. */
                if(linesize == 0)
                        numspace = 0;
                else if ((line[linesize-1] == '.' ||
                                        line[linesize-1] == '?' ||
                                        line[linesize-1] == '!') &&
                                isupper(word[0]))
                        numspace = 2;
                else
                        numspace = 1;

                /* See if it fits on the line, complete with separating
                   space. */
                if(linesize + wordsize + numspace > WIDTH)
                {
                        /* Too long.  Spit out the line, and reset. */
                        printf("%s\n", line);
                        strcpy(line, word);
                        linesize = wordsize;
                }
                else
                {
                        /* Fits.  Add it to the line. */
                        strcat(line, "  " + (2 - numspace));
                        strcat(line, word);
                        linesize += wordsize + numspace;
                }
        }

        /* Print the last line. */
        printf("%s\n", line);
}


This is a Simple String Formatter


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 replacing each tab by the string “Tab”,
 Breaking the String into Words
 search for string in a cell array
 Trim string in JavaScript
 How to read string text in files
 add string to another, Lowercase ,Uppercase
 search in a string and replace
 get number of words in a string
 Check empty string
 @Enumerated and EnumType.STRING

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team