code which reverses a string of characters....just need some feed back.........

Discussion in 'C Programming' started by Ceriousmall, Mar 28, 2011.

  1. Ceriousmall

    Ceriousmall Guest

    I'm just putting this out to get some general feed
    back..........................

    /* 2011 Ceriousmall
    This piece of code reverses the character string using the
    function reverse(s) */

    #include <stdio.h>

    #define MAXLINE 1000 /* maximum input line size */

    /* assigns the character string to ln[] */
    int gotline(char ln[])
    {
    int c, i;
    int at_end;

    for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
    +i)
    ln = c;
    at_end = c == EOF;

    if (c == '\n') {
    ln = c;
    ++i;
    }
    ln = '\0';

    if (at_end)
    return EOF;
    else
    return i;
    }

    /* reverses the character string s[] */
    void reverse(char s[])
    {
    int i, x;
    char subline[MAXLINE];

    for (x = 0; s[x] != '\0'; ++x)
    if (s[x] == '\n') {
    i = x;
    --i;
    }
    else
    i = x;

    for (x = 0; i >= 0; ++x && --i) {
    subline[x] = s;
    subline = s[x];
    }
    for (i = 0; i < x; ++i)
    s = subline;
    }

    int main(void)
    {
    int return_val;
    int at_start, at_end;
    char line[MAXLINE];

    at_start = 0;

    while (at_start != EOF) {
    return_val = gotline(line);
    at_end = return_val == EOF;
    reverse(line);
    printf("\n%s", line);
    putchar('\n');

    if (at_end)
    at_start = EOF;
    }
    return 0;
    }

    I've also found a better way to do the flip on the text stream in
    reverse[] but its not
    entirely my own idea but i was heading in that general direction,
    this was just the nudge i needed.

    /* reverses the character string s[] */
    void reverse(char s[])
    {
    int i, x;
    char chr;

    for (x = 0; s[x] != '\0'; ++x)
    if (s[x] == '\n') {
    i = x;
    --i;
    }
    else
    i = x;

    for (x = 0; x < i; ++x && --i) {
    chr = s[x];
    s[x] = s;
    s = chr;
    }
    }
     
    Ceriousmall, Mar 28, 2011
    #1
    1. Advertising

  2. Ceriousmall

    Mark Bluemel Guest

    Re: code which reverses a string of characters....just need somefeed back.........

    On 03/28/2011 08:24 AM, Ceriousmall wrote:
    > I'm just putting this out to get some general feed
    > back..........................


    Haven't you heard of fgets() or strlen()?
     
    Mark Bluemel, Mar 28, 2011
    #2
    1. Advertising

  3. Re: code which reverses a string of characters....just need somefeed back.........

    On 2011-03-28 09:24, Ceriousmall wrote:
    > I'm just putting this out to get some general feed
    > back..........................
    >
    > /* 2011 Ceriousmall
    > This piece of code reverses the character string using the
    > function reverse(s) */
    >
    > #include<stdio.h>

    [...]

    Note that there is a much shorter (but slower) solution using recursion:

    #include <stdio.h>

    void reverse(void)
    {
    int ch;

    ch = getchar();
    if (ch != '\n') {
    reverse();
    putchar(ch);
    }
    }


    int main(void)
    {
    int ch;

    ch = getchar();
    while (ch != EOF) {
    ungetc(ch, stdin);
    reverse();
    putchar('\n');
    ch = getchar();
    }
    return 0;
    }


    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
     
    August Karlstrom, Mar 28, 2011
    #3
  4. Re: code which reverses a string of characters....just need somefeed back.........

    On 2011-03-28 13:24, August Karlstrom wrote:
    [...]
    > #include <stdio.h>
    >
    > void reverse(void)
    > {
    > int ch;
    >
    > ch = getchar();
    > if (ch != '\n') {


    We may want to replace the last line above with

    if ((ch != EOF) && (ch != '\n')) {

    in order to handle files without newline ending.


    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
     
    August Karlstrom, Mar 28, 2011
    #4
  5. Ceriousmall <> writes:

    > I'm just putting this out to get some general feed
    > back..........................
    >
    > /* 2011 Ceriousmall
    > This piece of code reverses the character string using the
    > function reverse(s) */
    >
    > #include <stdio.h>
    >
    > #define MAXLINE 1000 /* maximum input line size */
    >
    > /* assigns the character string to ln[] */
    > int gotline(char ln[])


    It's much more useful to have a function that can be told how long the
    array is with a second parameter.

    > {
    > int c, i;
    > int at_end;
    >
    > for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
    > +i)
    > ln = c;
    > at_end = c == EOF;
    >
    > if (c == '\n') {
    > ln = c;
    > ++i;
    > }
    > ln = '\0';
    >
    > if (at_end)
    > return EOF;
    > else
    > return i;


    The use of at_end seems a little over the top. You could eliminate it
    altogether and just end the function with

    return c == EOF ? EOF : i;

    > }
    >
    > /* reverses the character string s[] */


    This does not do what is says. A reader used to C will expect that all
    the character are reversed, but you specifically exclude a terminating
    newline.

    > void reverse(char s[])
    > {
    > int i, x;
    > char subline[MAXLINE];
    >
    > for (x = 0; s[x] != '\0'; ++x)
    > if (s[x] == '\n') {
    > i = x;
    > --i;


    i = x - 1;

    > }
    > else
    > i = x;


    There's no need to set i every time. In fact you should be using
    strlen.

    >
    > for (x = 0; i >= 0; ++x && --i) {


    Its clearer to write ++x, --i rather than use &&.

    > subline[x] = s;
    > subline = s[x];
    > }
    > for (i = 0; i < x; ++i)
    > s = subline;
    > }
    >
    > int main(void)
    > {
    > int return_val;
    > int at_start, at_end;
    > char line[MAXLINE];
    >
    > at_start = 0;
    >
    > while (at_start != EOF) {
    > return_val = gotline(line);
    > at_end = return_val == EOF;
    > reverse(line);
    > printf("\n%s", line);
    > putchar('\n');
    >
    > if (at_end)
    > at_start = EOF;
    > }
    > return 0;
    > }


    Again, rather too any extra variables for my taste and it's a shame that
    the length of the string (which you know form gotline) is not used --
    you end up re-scannign it in the reverse function.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Mar 28, 2011
    #5
  6. Ceriousmall

    Ceriousmall Guest

    Thanks for the feed back guys............. I even missed a few
    elementary things.............
     
    Ceriousmall, Mar 28, 2011
    #6
  7. Ceriousmall

    Lew Pitcher Guest

    On March 28, 2011 03:24, in comp.lang.c, wrote:

    > I'm just putting this out to get some general feed
    > back..........................
    >
    > /* 2011 Ceriousmall
    > This piece of code reverses the character string using the
    > function reverse(s) */
    >
    > #include <stdio.h>
    >
    > #define MAXLINE 1000 /* maximum input line size */
    >
    > /* assigns the character string to ln[] */
    > int gotline(char ln[])
    > {
    > int c, i;
    > int at_end;
    >
    > for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
    > +i)
    > ln = c;
    > at_end = c == EOF;
    >
    > if (c == '\n') {
    > ln = c;
    > ++i;
    > }
    > ln = '\0';
    >
    > if (at_end)
    > return EOF;
    > else
    > return i;
    > }
    >
    > /* reverses the character string s[] */
    > void reverse(char s[])

    [snip]

    void reverse(char s[])
    char *front, *back;

    /* find the end of the string */
    for (back = s; *back != 0; ++back) {/*nop*/}

    /* swap front with back, move both toward middle */
    for (front = s; front+1 < back; ++front, --back)
    {
    char temp;

    temp = *(back-1);
    *(back-1) = *front;
    *front = temp;
    }
    }

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Mar 29, 2011
    #7
  8. Ceriousmall

    Lew Pitcher Guest

    Oops.... copy&paste error

    On March 28, 2011 21:14, in comp.lang.c, wrote:

    > On March 28, 2011 03:24, in comp.lang.c, wrote:
    >
    >> I'm just putting this out to get some general feed
    >> back..........................
    >>
    >> /* 2011 Ceriousmall
    >> This piece of code reverses the character string using the
    >> function reverse(s) */
    >>
    >> #include <stdio.h>
    >>
    >> #define MAXLINE 1000 /* maximum input line size */
    >>
    >> /* assigns the character string to ln[] */
    >> int gotline(char ln[])
    >> {
    >> int c, i;
    >> int at_end;
    >>
    >> for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
    >> +i)
    >> ln = c;
    >> at_end = c == EOF;
    >>
    >> if (c == '\n') {
    >> ln = c;
    >> ++i;
    >> }
    >> ln = '\0';
    >>
    >> if (at_end)
    >> return EOF;
    >> else
    >> return i;
    >> }
    >>
    >> /* reverses the character string s[] */
    >> void reverse(char s[])

    > [snip]


    void reverse(char s[])
    {
    char *front, *back;

    /* find the end of the string */
    for (back = s; *back != 0; ++back) {/*nop*/}

    /* swap front with back, move both toward middle */
    for (front = s; front+1 < back; ++front, --back)
    {
    char temp;

    temp = *(back-1);
    *(back-1) = *front;
    *front = temp;
    }
    }

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Mar 29, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joby
    Replies:
    0
    Views:
    1,653
  2. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    so just hoew do you go about reading an rss feed?

    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=, Oct 7, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    388
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=
    Oct 7, 2005
  3. no
    Replies:
    0
    Views:
    423
  4. Protoman

    Rotor class reverses wierdly

    Protoman, Dec 30, 2006, in forum: C++
    Replies:
    1
    Views:
    453
    Jim Langston
    Dec 30, 2006
  5. Frank Spade

    IE reverses logic

    Frank Spade, Jan 11, 2005, in forum: Javascript
    Replies:
    11
    Views:
    160
    Richard Cornford
    Jan 13, 2005
Loading...

Share This Page