Thread: i dont want my nulls

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006

    Smile i dont want my nulls

    hello everyone!!
    i need a little help if somebody can oblige.

    I'm trying to copy one small string into a much larger one several times over
    using strcat(). This works fine however it also includes the \0 on the end of the
    shorter string. I would like to remove this if possible.
    Can somebody suggest a way of doing this or direct me to the 'how to' which i
    know i've seen somewhere on this very subject.


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Could you post code and a sample input/output that shows exactly what you are doing -- and what you want to do? And please use [code][/code] tags.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    New York
    strcat only functions on null terminated strings. You would have to write your own routine to do this, keeping track of your place in the larger string, because you would not be able to just seek to the end like you would be able to with a null-terminated string. I can't see why you wouldn't want a null-terminated string for this.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    I don't understand your problem. strcat will overwrite the existing null character when it concatenates the new string. There shouldn't be any problem unless you're not working with strings, in which case strcat is the wrong solution. memcpy would be better.
    My best code is written with the delete key.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    Quote Originally Posted by the bassinvader
    This works fine however it also includes the \0 on the end of the
    shorter string.
    Are you sure? It shouldn't. The function strcat() copies over the null at the end of the string being copied to.
    #include <stdio.h>
    #include <string.h>
    int main(void) {
       char sString[] = "Hello";
       char lString[41];
       while(strlen(lString) + strlen(sString) < 40) {
          strcat(lString, sString);
       printf("%s", lString);
       return 0;
      [H][e][l][l][o][\0]  ... strcpy ...
                     [H][e][l][l][o][\0]  ... giving you ...
    ...and a billion people already replied. Note to self: Write shorter examples.
    Sent from my iPadŽ

  6. #6
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    well maybe i've asked the wrong question to solve the problem!

    i want to take a short string like this:
    " a short string "

    and copy it into a larger string so it looks like this:

    " a short stringa short stringa short stringa short string "

    however at the moment when i 'printf' my long string i get this:

    a short string
    a short string
    a short string


    int i;
    char longstring[100]; /*holds nothing to start*/
    char short[20]="a short string";


    int mon;


    this code does exactly what i want. However if i get the code from the user and dont know the length
    of the string i get the problem.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    The problem sounds like you are reading in a string using fgets or some other
    function which reads in a newline. It's quite easy to get rid of the newline,
    see my sig for a link on how to use fgets in that manner.

    the code you posted has an error:


    variable "i" is uninitialised at this point. You also didn't use code tags properly,
    it's a /, not a \ to terminate the tags!

    >>dont know the length of the string...

    You can use strlen to get the length of a string, in case you didn't know already.
    No No's:
    fflush (stdin); gets (); void main ();

    Example of fgets (); The FAQ, C/C++ Reference

    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition

    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. CRecordset and nulls
    By redfiche in forum Windows Programming
    Replies: 3
    Last Post: 08-02-2005, 09:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. pointers into largish text files
    By donkeypunch in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2004, 03:04 PM
  5. Replies: 9
    Last Post: 01-21-2004, 04:53 AM