home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19157 < prev    next >
Encoding:
Text File  |  1993-01-03  |  1.6 KB  |  55 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!think.com!rpi!batcomputer!cornell!uw-beaver!uw-coco!quick!amc-gw!pilchuck!parody!dietz
  3. From: dietz@parody.Data-IO.COM (Kent Dietz)
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <1993Jan3.194154.1740@data-io.com>
  6. Sender: news@data-io.com (The News)
  7. Organization: Data I/O Corporation
  8. References:  <1993Jan2.233446.20833@iitmax.iit.edu>
  9. Date: Sun, 3 Jan 1993 19:41:54 GMT
  10. Lines: 43
  11.  
  12. In article <1993Jan2.233446.20833@iitmax.iit.edu>, matujos@elof.iit.edu (Joe Matusiewicz) writes:
  13. |> 
  14. |>    I am having a little problem with string processing.  I have been
  15. |> programming in C for 6 months, and I just can't figure out the following
  16. |> problem:
  17. |> 
  18. |>    I need a function to append a character to the end of a string.
  19.  
  20. Try using sprintf():
  21.  
  22.     sprintf(str, "%s%c", str, ch);
  23.  
  24. This assumes that str is large enough to hold the new character!
  25.  
  26. Probably what is wrong with the following code (which you should not
  27. use in my humble opinion) is that calloc syntax is different than 
  28. your usage:
  29.  
  30.     char *calloc(num_of_elem, elem_size)
  31.     
  32. I suspect that got things confused.
  33.  
  34. |> I can't find an ANSI function to do so, so I tried to write my own.
  35. |> Here's what I have:
  36. |> 
  37. |> 
  38. |>          void add_char_to_str(char ch, char *str)
  39. |>          {
  40. |> 1.           char *tmp;
  41. |> 2.           tmp = (char *) calloc (2*sizeof(char));
  42. |> 3.           *tmp = ch;
  43. |> 4.           *(tmp+1) = '\0';
  44. |> 5.           strcat(str, tmp);
  45. |>              free (tmp);
  46. |>          }
  47. |> 
  48. |>                                    Joe.
  49.  
  50.  
  51. Good Luck,
  52.  
  53. Kent Dietz
  54. Data I/O Corporation
  55.