reverse string, how to print string and not decimals?

Discussion in 'C Programming' started by ssecorp, Aug 5, 2008.

  1. ssecorp

    ssecorp Guest

    char* reverse(char* str) {
    int length = strlen(str);
    char* acc[length];
    int i;
    for (i=0; i<=length-1; i++){
    acc[length-1-i] = str;
    }
    return acc;
    }

    How do I then print a string and not decimals? If I add printf inside
    the for loop it prints decimals. they are reversed so it works but I
    want to prin chars.
     
    ssecorp, Aug 5, 2008
    #1
    1. Advertising

  2. ssecorp

    Default User Guest

    ssecorp wrote:

    > char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];
    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;
    > }
    > return acc;
    > }
    >
    > How do I then print a string and not decimals? If I add printf inside
    > the for loop it prints decimals. they are reversed so it works but I
    > want to prin chars.


    Show us the code. There is no printf() in what you have.




    Brian
     
    Default User, Aug 5, 2008
    #2
    1. Advertising

  3. "Default User" <> wrote in message
    news:...

    >> char* acc[length];


    Oops!
     
    Mark B [Diputsur], Aug 5, 2008
    #3
  4. ssecorp

    Default User Guest

    Mark B [Diputsur] wrote:

    >
    > "Default User" <> wrote in message
    > news:...
    >
    > >> char* acc[length];

    >
    > Oops!


    I most certainly did NOT write that.



    Brian
     
    Default User, Aug 5, 2008
    #4
  5. ssecorp

    Lew Pitcher Guest

    In comp.lang.c, ssecorp wrote:

    > char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];


    acc is an array of pointers to characters. Are you /positive/ that this is
    what you want. I don't think it is.

    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;
    > }
    > return acc;


    Are you /certain/ that you want to return an array of pointers to
    characters? Wouldn't you rather return a single pointer to a character?
    > }


    > How do I then print a string and not decimals?


    Huh?

    > If I add printf inside the for loop it prints decimals.


    Then you did it wrong. In any case, it makes no sense to put a printf()
    inside your reversal loop.

    > they are reversed so it works but I want to prin chars.


    Yes? So?

    First off, correct your string reversal function. You want something like...

    char *reverse(char *str)
    {
    char *start, *end, temp;

    for (end=str;*end;++end); /* point end at end-of-string */
    --end; /* point end at last-character-in-string */
    for (start=str; /* point start at first-character-in-string */
    start < end; /* loop until start passes end */
    ++start, --end) /* move start up, end down */
    {
    temp = *start; /* save the character at start */
    *start = *end; /* put character at end into place at start */
    *end = temp; /* put saved start character into place at end */
    }
    return str; /* return pointer to first character */
    }

    Then, you can do something like...

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    char text[] = "This is a test";

    printf("Normal: \"%s\"\n",text);
    printf("Reversed: \"%s\"\n",reverse(text));

    return EXIT_SUCCESS;
    }

    and see that the reverse() function does indeed reverse any modifiable text
    string.

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Aug 5, 2008
    #5
  6. "Default User" <> wrote in message
    news:...
    > Mark B [Diputsur] wrote:
    >
    >>
    >> "Default User" <> wrote in message
    >> news:...
    >>
    >> >> char* acc[length];

    >>
    >> Oops!

    >
    > I most certainly did NOT write that.


    No, but you quoted it, and rather than looking
    at the code and finding the error, you asked for
    additional information, while his problem was
    looking you right in the face... no?
     
    Mark B [Diputsur], Aug 5, 2008
    #6
  7. ssecorp

    Guest

    On Aug 5, 3:19 pm, ssecorp <> wrote:
    > char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];
    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;
    > }
    > return acc;
    >
    > }
    >
    > How do I then print a string and not decimals? If I add printf inside
    > the for loop it prints decimals. they are reversed so it works but I
    > want to prin chars.


    What do you mean by "decimals"? If you're using printf() like
    'printf("%d", str)', then yes, it will print something like
    numbers, since the conversion specification '%d' is for integers, not
    for chars. Use '%c', or simply putchar().

    Also, your function returns a pointer to a local object, which will be
    deallocated when the function returns, and thus what it returns will
    point to nothing. What you want is probably a function that modifies
    the string "in place."

    Sebastian
     
    , Aug 5, 2008
    #7
  8. ssecorp <> wrote:
    > char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];


    This is problematic for at least four reasons:

    a) The size of an array must be a compile-time constant unless
    you have a compiler that supports C99 variable length arrays
    b) You ask for 'length' pointers to char, but it looks very
    much as if you just want an array of chars
    c) If 'acc' would be an array of chars it would be too short by
    one element if you want to store a string with the same length
    as that of 'str' in it (don't forget about the trailing '\0'
    char that is needed to make a string out of a mere char array)
    d) You later try to pass back a pointer to the array to the caller
    but this array is a local variable that vanishes the moment the
    function is left and then can't be used anymore.

    There are basically two ways you can get aroung the last problem.

    1) Have the caller pass a second array to the function in which
    you store the reversed string
    2) Allocate memory for the reversed string (and tell everyone
    that's going to use this function to deallocate it when it's
    not needed anymore).

    A third approach would be to have a static array that's long
    enough for each string you will pass to this function. But there
    are two difficulties. First of all knowing in advance what the
    longest string that ever will be passed to the function can be
    tricky ti say the least. And the reversed string can only be used
    between two invokations of the function since the next invokation
    will overwrite the previous result.

    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;
    > }


    If 'acc' would be an array of chars (of sufficient length) it
    now would contain the reversed chars from 'str'. But it's still
    missing a trailing '\0' char and thus it's not a string.

    > return acc;


    And that's, as I wrote, absolutely wrong. You can't safely pass
    back a pointer to a local variable for use in the caller.

    > }


    > How do I then print a string and not decimals? If I add printf inside
    > the for loop it prints decimals. they are reversed so it works but I
    > want to prin chars.


    No idea what this means. Show your printf() call and you probably
    will get an answer. But without knowing what exactly you did there
    it's impossible to say.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 5, 2008
    #8
  9. ssecorp

    Default User Guest

    Mark B [Diputsur] wrote:

    >
    > "Default User" <> wrote in message
    > news:...
    > > Mark B [Diputsur] wrote:
    > >
    > > >
    > >>"Default User" <> wrote in message
    > > > news:...
    > > >
    > >>>> char* acc[length];
    > > >
    > > > Oops!

    > >
    > > I most certainly did NOT write that.

    >
    > No, but you quoted it,


    So? You removed the attribution from the person who DID write it and
    said that I did.

    > and rather than looking
    > at the code and finding the error, you asked for
    > additional information, while his problem was
    > looking you right in the face... no?


    No. Without complete code that has a chance of demonstrating his
    problem, why waste my time?



    Brian
     
    Default User, Aug 5, 2008
    #9
  10. ssecorp

    Default User Guest

    Richard Heathfield wrote:

    > Default User said:
    >
    > > Mark B [Diputsur] wrote:
    > >
    > >>
    > >> "Default User" <> wrote in message
    > >> news:...
    > >>
    > >> >> char* acc[length];
    > >>
    > >> Oops!

    > >
    > > I most certainly did NOT write that.

    >
    > Right - you merely quoted it, and we all do that, obviously.
    > (Use-mention distinction.)



    I fail to understand your point.




    Brian
     
    Default User, Aug 5, 2008
    #10
  11. On Tue, 5 Aug 2008 13:19:39 -0700 (PDT), ssecorp
    <> wrote:

    >char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];
    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;


    Due to what is probably the wrong definition of acc, this statement
    contains a constraint violation and the compiler must issue a
    diagnostic for it. Did yours? If so, why did you ignore it? If not,
    either up the warning level or upgrade to a competent compiler.

    > }
    > return acc;
    >}
    >
    >How do I then print a string and not decimals? If I add printf inside
    >the for loop it prints decimals. they are reversed so it works but I
    >want to prin chars.


    You need to tell us what code you executed and how the result differed
    from what you want. The fact that you used printf is not sufficient.
    Show us the exact code.

    --
    Remove del for email
     
    Barry Schwarz, Aug 6, 2008
    #11
  12. ssecorp

    santosh Guest

    Default User wrote:
    > Richard Heathfield wrote:
    >> Default User said:
    >> > Mark B [Diputsur] wrote:
    >> >> "Default User" <> wrote in message
    >> >> news:...
    >> >>
    >> >> >> char* acc[length];
    >> >>
    >> >> Oops!
    >> >
    >> > I most certainly did NOT write that.

    >>
    >> Right - you merely quoted it, and we all do that, obviously.
    >> (Use-mention distinction.)

    >
    > I fail to understand your point.


    I think the point is that the quote markers clearly indicate that you
    did not write the "char* acc[length];" line, despite "Mark B
    [Diputsur]" deleting the correct attribution line and retaining the
    wrong one. So it was, strictly speaking, unnecessary for you to respond
    to him, since he wasn't quoting you in the first place.
     
    santosh, Aug 6, 2008
    #12
  13. "ssecorp" <> wrote in message
    news:...
    > char* reverse(char* str) {
    > int length = strlen(str);
    > char* acc[length];
    > int i;
    > for (i=0; i<=length-1; i++){
    > acc[length-1-i] = str;
    > }
    > return acc;
    > }
    >
    > How do I then print a string and not decimals? If I add printf inside
    > the for loop it prints decimals. they are reversed so it works but I
    > want to prin chars.



    I have no idea what you want, but here is something you can look at:
    ________________________________________________________________
    #include <stdio.h>
    #include <string.h>


    static char*
    reverse_in_place(
    char* const buf
    ) {
    char* spos = buf;
    char* epos = spos + strlen(spos) - 1;
    for (; spos < epos; ++spos, --epos) {
    char const tmp = *spos;
    *spos = *epos;
    *epos = tmp;
    }
    return buf;
    }


    static void
    fprint_without_char(
    FILE* const out,
    char const* buf,
    char const strip
    ) {
    for (; *buf; ++buf) {
    if (*buf != strip || strip == '\0') {
    fputc(*buf, out);
    }
    }
    }


    int main() {
    char data1[] = "\nsdoirep tuohtiw - ...1 .rev .gnirts .esrever";
    char data2[] = "\nsdoirep htiw - ...1 .rev .gnirts .esrever";
    fprint_without_char(stdout, reverse_in_place(data1), '.');
    fprint_without_char(stdout, reverse_in_place(data2), '\0');
    return 0;
    }
    ________________________________________________________________



    You can reverse the strings and get the output to strip out the periods via
    the `fprint_without_char()' procedure...
     
    Chris M. Thomasson, Aug 6, 2008
    #13
  14. "Chris M. Thomasson" <> wrote in message
    news:ix9mk.6564$...
    [...]
    > static void
    > fprint_without_char(
    > FILE* const out,
    > char const* buf,
    > char const strip
    > ) {
    > for (; *buf; ++buf) {
    > if (*buf != strip || strip == '\0') {
    > fputc(*buf, out);
    > }
    > }
    > }


    A more "efficient" version of the code above would be:
    __________________________________________________________________
    static void
    fprint_without_char(
    FILE* const out,
    char const* buf,
    char const strip
    ) {
    if (strip) {
    for (; *buf; ++buf) {
    if (*buf != strip) {
    fputc(*buf, out);
    }
    }
    } else {
    fprintf(out, "%s", buf);
    }
    }
    __________________________________________________________________

    [...]
     
    Chris M. Thomasson, Aug 6, 2008
    #14
  15. ssecorp

    Default User Guest

    Richard Heathfield wrote:

    > Default User said:


    > > So? You removed the attribution from the person who DID write it

    >
    > Yes, he did, but...
    >
    > > and said that I did.

    >
    > ...no, he didn't do that, not as far as anyone is concerned who
    > retains the ability to count quote-markers.


    I'm sorry, but that's a crock.




    Brian
     
    Default User, Aug 6, 2008
    #15
  16. ssecorp

    Default User Guest

    santosh wrote:

    > Default User wrote:
    > > Richard Heathfield wrote:
    > >> Default User said:
    > >> > Mark B [Diputsur] wrote:
    > >> >> "Default User" <> wrote in message
    > >> >> news:...
    > >> >>
    > >> >> >> char* acc[length];
    > >> >>
    > >> >> Oops!
    > >> >
    > >> > I most certainly did NOT write that.
    > >>
    > >> Right - you merely quoted it, and we all do that, obviously.
    > >> (Use-mention distinction.)

    > >
    > > I fail to understand your point.

    >
    > I think the point is that the quote markers clearly indicate that you
    > did not write the "char* acc[length];" line


    I don't believe that quote markers DO adequately indicate that.




    Brian
     
    Default User, Aug 6, 2008
    #16
  17. "Default User" <> wrote in message
    news:...
    > Richard Heathfield wrote:
    >
    >> Default User said:

    >
    >> > So? You removed the attribution from the person who DID write it

    >>
    >> Yes, he did, but...
    >>
    >> > and said that I did.

    >>
    >> ...no, he didn't do that, not as far as anyone is concerned who
    >> retains the ability to count quote-markers.

    >
    > I'm sorry, but that's a crock.


    I apologize if my snip insulted you, but IMHO you
    did inherit his mistake with your response.

    > No. Without complete code that has a chance of demonstrating his
    > problem, why waste my time?


    You had complete code which demonstrated his problem.
    The line of code he stuck in for debugging purposes when
    the function didn't perform properly sidetracks the issue,
    why waste your time asking to see it?
     
    Mark B [Diputsur], Aug 6, 2008
    #17
  18. ssecorp

    Default User Guest

    Mark B [Diputsur] wrote:

    >
    > "Default User" <> wrote in message
    > news:...
    > > Richard Heathfield wrote:
    > >
    > > > Default User said:

    > >
    > >>> So? You removed the attribution from the person who DID write it
    > > >
    > > > Yes, he did, but...
    > > >
    > >>> and said that I did.
    > > >
    > > > ...no, he didn't do that, not as far as anyone is concerned who
    > > > retains the ability to count quote-markers.

    > >
    > > I'm sorry, but that's a crock.

    >
    > I apologize if my snip insulted you,


    Learn basic netiquette.

    > but IMHO you
    > did inherit his mistake with your response.


    Bullshit. I specifically said that I wasn't going to comment because he
    didn't provide a complete example.




    Brian
     
    Default User, Aug 6, 2008
    #18
  19. "Default User" <> wrote in message
    news:...
    > Mark B [Diputsur] wrote:
    >
    >>
    >> "Default User" <> wrote in message
    >> news:...
    >> > Richard Heathfield wrote:
    >> >
    >> > > Default User said:
    >> >
    >> >>> So? You removed the attribution from the person who DID write it
    >> > >
    >> > > Yes, he did, but...
    >> > >
    >> >>> and said that I did.
    >> > >
    >> > > ...no, he didn't do that, not as far as anyone is concerned who
    >> > > retains the ability to count quote-markers.
    >> >
    >> > I'm sorry, but that's a crock.

    >>
    >> I apologize if my snip insulted you,

    >
    > Learn basic netiquette.


    **** you. I've been civil this entire time and posting
    on usenet (including brief stints on this board) since 1999.

    >> but IMHO you
    >> did inherit his mistake with your response.

    >
    > Bullshit. I specifically said that I wasn't going to comment because he
    > didn't provide a complete example.


    Where? What you said was:
    "Show us the code. There is no printf() in what you have."

    Looks to me like you examined the code, and didn't see a printf()
    statement. You're a liar, and an asshole, and I'm done with you.

    Good day sir.
     
    Mark B [Diputsur], Aug 6, 2008
    #19
  20. ssecorp

    Richard Guest

    "Default User" <> writes:

    > Mark B [Diputsur] wrote:
    >
    >>
    >> "Default User" <> wrote in message
    >> news:...
    >> > Richard Heathfield wrote:
    >> >
    >> > > Default User said:
    >> >
    >> >>> So? You removed the attribution from the person who DID write it
    >> > >
    >> > > Yes, he did, but...
    >> > >
    >> >>> and said that I did.
    >> > >
    >> > > ...no, he didn't do that, not as far as anyone is concerned who
    >> > > retains the ability to count quote-markers.
    >> >
    >> > I'm sorry, but that's a crock.

    >>
    >> I apologize if my snip insulted you,

    >
    > Learn basic netiquette.
    >
    >> but IMHO you
    >> did inherit his mistake with your response.

    >
    > Bullshit. I specifically said that I wasn't going to comment because he
    > didn't provide a complete example.
    >
    >
    >
    >
    > Brian


    Calm down Bwian. I think you've had your quota of nagging people about
    how and what to post today don't you? No wonder Heathfield kicked your
    backside about it last year.
     
    Richard, Aug 6, 2008
    #20
    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. dogbite
    Replies:
    4
    Views:
    727
    osmium
    Oct 10, 2003
  2. Gary Wessle

    print up to n decimals

    Gary Wessle, Jul 27, 2006, in forum: C++
    Replies:
    4
    Views:
    402
    Victor Bazarov
    Jul 27, 2006
  3. keto
    Replies:
    0
    Views:
    1,043
  4. Chris Dollin

    Re: Reverse and print a string

    Chris Dollin, May 1, 2008, in forum: C Programming
    Replies:
    3
    Views:
    239
    santosh
    May 1, 2008
  5. santosh

    Re: Reverse and print a string

    santosh, May 1, 2008, in forum: C Programming
    Replies:
    2
    Views:
    305
    Kevin D. Quitt
    May 2, 2008
Loading...

Share This Page