home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!think.com!rpi!batcomputer!cornell!uw-beaver!uw-coco!quick!amc-gw!pilchuck!parody!dietz
- From: dietz@parody.Data-IO.COM (Kent Dietz)
- Subject: Re: Problem with string processing.
- Message-ID: <1993Jan3.194154.1740@data-io.com>
- Sender: news@data-io.com (The News)
- Organization: Data I/O Corporation
- References: <1993Jan2.233446.20833@iitmax.iit.edu>
- Date: Sun, 3 Jan 1993 19:41:54 GMT
- Lines: 43
-
- In article <1993Jan2.233446.20833@iitmax.iit.edu>, matujos@elof.iit.edu (Joe Matusiewicz) writes:
- |>
- |> I am having a little problem with string processing. I have been
- |> programming in C for 6 months, and I just can't figure out the following
- |> problem:
- |>
- |> I need a function to append a character to the end of a string.
-
- Try using sprintf():
-
- sprintf(str, "%s%c", str, ch);
-
- This assumes that str is large enough to hold the new character!
-
- Probably what is wrong with the following code (which you should not
- use in my humble opinion) is that calloc syntax is different than
- your usage:
-
- char *calloc(num_of_elem, elem_size)
-
- I suspect that got things confused.
-
- |> I can't find an ANSI function to do so, so I tried to write my own.
- |> Here's what I have:
- |>
- |>
- |> void add_char_to_str(char ch, char *str)
- |> {
- |> 1. char *tmp;
- |> 2. tmp = (char *) calloc (2*sizeof(char));
- |> 3. *tmp = ch;
- |> 4. *(tmp+1) = '\0';
- |> 5. strcat(str, tmp);
- |> free (tmp);
- |> }
- |>
- |> Joe.
-
-
- Good Luck,
-
- Kent Dietz
- Data I/O Corporation
-