Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Format string function

Format string function

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)

    T R 2 Replies Last reply
    0
    • A Anonymous

      Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      the screen driver does this automatically. anyway, you could do the simple (according that your string is str, C-style string):

      int iCpt = 0;
      while (str[iCpt] != '\0') {
      if (!(iCpt%80)) {
      printf("\n");
      }
      printf("%c", str[iCpt]);
      iCpt++;
      }

      or the more complex code below (i'm not even sure it works) :

      int iCpt = 0, iResult = 80;
      while (iResult == 80) {
      iResult = printf("%80s\n", str[iCpt]);
      iCpt += iResult;
      }


      TOXCCT >>> GEII power

      A 1 Reply Last reply
      0
      • T toxcct

        the screen driver does this automatically. anyway, you could do the simple (according that your string is str, C-style string):

        int iCpt = 0;
        while (str[iCpt] != '\0') {
        if (!(iCpt%80)) {
        printf("\n");
        }
        printf("%c", str[iCpt]);
        iCpt++;
        }

        or the more complex code below (i'm not even sure it works) :

        int iCpt = 0, iResult = 80;
        while (iResult == 80) {
        iResult = printf("%80s\n", str[iCpt]);
        iCpt += iResult;
        }


        TOXCCT >>> GEII power

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        I do not want to break up any whole words. I can show you. If my maximum width is 8 characters and this is my text: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh I want to have this: aaa bbbb\nccccc\nddddd\neeeeeee\nfff gg\nhhhhh So my result would be this: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh

        T 1 Reply Last reply
        0
        • A Anonymous

          I do not want to break up any whole words. I can show you. If my maximum width is 8 characters and this is my text: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh I want to have this: aaa bbbb\nccccc\nddddd\neeeeeee\nfff gg\nhhhhh So my result would be this: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          ahh ok, well if you don't say it, how should i guess... hem, i'm thinking of this : * add a marker to record the last whitespace you encountered before the end of the line, * create a buffer 81 caracters long (because of the trailing '\0') where you copy the caracters (and so WORDS) to printf, * when you encountered the end of a line, or the end of the source string, copy the buffer to the screen


          TOXCCT >>> GEII power

          1 Reply Last reply
          0
          • A Anonymous

            Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)

            R Offline
            R Offline
            ravjak
            wrote on last edited by
            #5

            suppose your string is char str[]="......" #define MIN(a, b) (((a) < (b)) ? (a) : (b)) int str_len = strlen(str); int LINE_LEN=7; //you will nead here 80 int c=(int)ceil((float)str_len/(float)LINE_LEN); char *str2= new char[str_len+c+1]; int n=0; int m=0; for(int k=0;k Pain is a weakness living the body

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups