Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #140149
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Function to trim trailing spaces |
| Date | Thu, 24 Jan 2019 02:13:12 -0800 |
| Organization | None to speak of |
| Lines | 81 |
| Message-ID | <ln5zue1hzb.fsf@kst-u.example.com> (permalink) |
| References | <p_82E.54640$G14.18982@fx29.iad> <549145a8-950f-4027-9495-b909ab34a6a9@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="408bb0283150d2fd02799035b8169951"; logging-data="24788"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kP096BGS4aSUN852Q9cSI" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:GwSl5Or05YI060uCNbHiS8tKNEY= sha1:P1ReAw9xmHrm1GJCGUcmftxB99s= |
| Xref | csiph.com comp.lang.c:140149 |
Show key headers only | View raw
mark.bluemel@gmail.com writes:
[...]
> As already mentioned, there are enough obvious ones to
> be going on with.
>
> If ever there was an example of the need for Test
> Driven Development, this looks like it - not because
> the task is hard, but because programmers need to be
> made to think.
>
> Please tell us what happens when you run the following
> snippets.
>
> char *s = "my dog has fleas";
> printf("Before: '%s'\n",str);
> rtrim(str);
> printf("After : '%s'\n",str);
It can fail because string literals are read-only. But who says rtrim()
need to work with read-only data? There needs to be some documentation
indicating whether it modifies the input or returns a pointer to a new
string. (The fact that the parameter is defined as char* and not const
char* is IMHO enough to document that fact. The fact that string
literals are not const is an unfortunate historical artifact.)
[...]
> Here's my 10-minute simplistic hack to do the job.
> The rtrim() function is not elegant, but suffices, and my main
> point is to show some basic ideas about how you could use testing.
> (My more complete example would test the results against expected
> values).
>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> char *rtrim(char *str) {
> char * result = strdup(str);
strdup() is defined by POSIX but not by ISO C. (It's easy enough to
write your own.)
> int i = strlen(result);
You didn't check whether the allocation failed.
> for (i = i-1; i >= 0 && result[i]==' '; i--) {
> result[i] = 0;
> }
> return result;
> }
>
> int main(void) {
>
> char * testStrings[] = {
> "my dog has no spaces",
> "my dog has one space ",
> "my dog has lots of spaces ",
> " ",
> " "
> };
>
> int i;
>
> for (i = 0; i < sizeof(testStrings)/sizeof(testStrings[0]); i++) {
> char *trimmed = rtrim(testStrings[i]);
> printf("before '%s' after '%s'\n", testStrings[i], trimmed);
> free(trimmed);
This version requires the caller to free the allocated memory, a
drawback that the original function doesn't have.
> }
>
> }
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 21:00 -0500
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-23 19:14 -0700
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 23:41 -0500
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-23 22:40 -0700
Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 10:44 +0000
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 11:20 -0500
Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 16:47 +0000
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 13:51 -0500
Re: Function to trim trailing spaces Manfred <noname@invalid.add> - 2019-01-24 17:49 +0100
Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 08:52 -0800
Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 17:40 +0000
Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 12:08 -0800
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:47 -0700
Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-25 14:40 +0000
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:44 -0700
Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-23 23:39 -0800
Re: Function to trim trailing spaces Rosario19 <Ros@invalid.invalid> - 2019-01-24 10:49 +0100
Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 01:55 -0800
Re: Function to trim trailing spaces "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-01-24 12:58 +0100
Re: Function to trim trailing spaces Nathan Wagner <nw@hydaspes.if.org> - 2019-01-24 03:01 +0000
Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-23 18:46 -0800
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 23:38 -0500
Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-23 23:39 -0800
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 11:25 -0500
Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 08:34 -0800
Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-24 15:05 -0800
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 18:40 -0500
Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 02:29 -0800
Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 15:06 -0500
Re: Function to trim trailing spaces Mateusz Viste <mateusz@wont.tell> - 2019-01-24 20:07 +0000
Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-24 20:24 +0000
Re: Function to trim trailing spaces Mateusz Viste <mateusz@wont.tell> - 2019-01-24 20:34 +0000
Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-24 21:37 +0000
Re: Function to trim trailing spaces Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2019-01-24 15:34 -0500
Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 13:19 -0800
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:52 -0700
Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 14:30 -0800
Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 16:59 -0700
Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 14:51 -0800
Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 01:06 -0800
Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 02:13 -0800
Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 03:46 -0800
Re: Function to trim trailing spaces Anton Shepelev <anton.txt@g{oogle}mail.com> - 2019-01-24 14:56 +0300
Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 05:14 -0800
Re: Function to trim trailing spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-01-24 18:51 +0000
Re: Function to trim trailing spaces "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-01-24 20:04 +0100
Re: Function to trim trailing spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-01-24 20:25 +0000
Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:50 +0300
Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:45 +0300
Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:54 +0300
Re: Function to trim trailing spaces Ralf Damaschke <rwspam@gmx.de> - 2019-01-24 22:43 +0000
Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 10:57 +0000
Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 23:05 +0000
Function to trim trailing spaces "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-01-24 03:53 -0800
csiph-web