String reverse function/ global array modification help

Discussion in 'C Programming' started by Tyler, Apr 9, 2012.

  1. Tyler

    Tyler Guest

    For starters here is the homework question:

    read a string and print it in reverse (25)
    reverseString function doesn't return anything
    use one parameter
    read a set of integers save them into an array and print the set in
    reverse (25)
    reverseInteger
    use two parameters (array and size)
    maximum array size is 100
    use a loop to read values, stop when a user enters 'y'
    Do Not print in reverse functions
    Bonus
    If you use pointers to swap values in reverse functions (10)
    if you implemented with multiple files and Makefile (10)

    essentially, I think my teacher wants us to modify the array/strings
    globally but I'm not really sure how to do that and I'm getting some
    funky errors with my code. However I think I'm really close.

    Here's my code:

    main.c

    /*
    * main.c
    * hw_L9
    *
    * Created by Charles Cothren on 4/8/12.
    * Copyright 2012 Syracuse University. All rights reserved.
    *
    */

    #include "reverseop.c"
    #include <stdio.h>

    int main ()
    {
    char part1string[100] = {0}, stop;
    int part2array[100] = {0}, i = 0;


    //Part 1


    printf("Enter the string you would lik to reverse: ");
    gets(part1string);

    reverseString(part1string);

    printf("Your string in reverse: %s", part1string);


    //Part 2


    printf("Integer Set Reverser\n");

    while( stop /= 'y')
    {
    printf("Enter an the next integer: ");
    scanf(" %d", part2array);
    i++;
    printf("Do you wish to stop? (y/n): ");
    scanf(" %c", &stop);
    }


    reverseInteger(part2array, i);

    printf("Your integers in reverse: %s", part2array);



    return 0;
    }

    reverseop.c


    #include <stdio.h>

    void reverseString(char *strng);
    void reverseInteger(int *arrayset[], int size);

    void reverseString(char *strng)
    {
    int q = 0, i;
    char strng2[strlen(strng)];


    for (i = (strlen(strng)-1); i >= 0; i--)
    {
    strng2[q] = strng;
    q++;
    }

    for (i = 0; i < (strlen(strng)-1); i++)
    {
    strng = strng2;
    }

    }

    void reverseInteger(arrayset[], size)
    {
    int q = 0, i;
    char arrayset2[size];

    for (i = (size-1); i >= 0; i--)
    {
    arrayset2[q] = arrayset;
    q++;
    }


    for (i = 0; i < (size-1); i++)
    {
    arrayset = arrayset2;
    }
    }

    I get the error expected ) before ] token on this line and I don't
    understand why

    void reverseInteger(arrayset[], size)
     
    Tyler, Apr 9, 2012
    #1
    1. Advertising

  2. Tyler

    Stefan Ram Guest

    Tyler <> writes:
    >read a string and print it in reverse (25)

    (...)
    >maximum array size is 100


    If the length of the input string is not limited, but the
    size of arrays is limited to 100, then complicated
    constructions have to be used to cope with long inputs.

    For example, when the string read has 2500 charactes, we
    might use an array of 50 pointers to arrays of 50 chars
    each to store the string.

    When the string read has 125000 characters, one can use an
    array of 50 pointers to arrays of 50 pointers to arrays of
    50 chars each.

    And so on.

    This is getting more complicated by the fact that the length
    of input is not known, when one starts to read it, so when
    one starts with an array of 50 chars, the structure of the
    input storage needs to be changed while reading more
    characters.

    But this is typical for programming: I/O and memory
    management take all the effort, while the actual
    Ā»computationĀ« (here: reversal) is not very much effort
    in comparison.

    SCNR
     
    Stefan Ram, Apr 9, 2012
    #2
    1. Advertising

  3. Tyler

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >Tyler <> writes:
    >>read a string and print it in reverse (25)

    >(...)
    >>maximum array size is 100

    >If the length of the input string is not limited, but the
    >size of arrays is limited to 100, then complicated
    >constructions have to be used to cope with long inputs.


    That is, of course, unless one is using something like:

    #include <stdio.h>

    void reverse( void )
    { int const c = getchar(); if( c != EOF ){ reverse(); putchar( c ); }}

    int main( void ){ reverse(); putchar( '\n' ); }
     
    Stefan Ram, Apr 9, 2012
    #3
  4. Tyler

    Eric Sosman Guest

    On 4/8/2012 10:38 PM, Tyler wrote:
    > For starters here is the homework question:
    >
    > read a string and print it in reverse (25)
    > reverseString function doesn't return anything
    > use one parameter
    > read a set of integers save them into an array and print the set in
    > reverse (25)
    > reverseInteger
    > use two parameters (array and size)
    > maximum array size is 100
    > use a loop to read values, stop when a user enters 'y'
    > Do Not print in reverse functions
    > Bonus
    > If you use pointers to swap values in reverse functions (10)
    > if you implemented with multiple files and Makefile (10)
    >
    > essentially, I think my teacher wants us to modify the array/strings
    > globally but I'm not really sure how to do that and I'm getting some
    > funky errors with my code. However I think I'm really close.


    It's really funky that you're feeling close.

    > Here's my code:
    >
    > main.c
    >
    > /*
    > * main.c
    > * hw_L9
    > *
    > * Created by Charles Cothren on 4/8/12.
    > * Copyright 2012 Syracuse University. All rights reserved.
    > *
    > */
    >
    > #include "reverseop.c"


    Although this is technically legal, it's really not a good way
    to do things. The purpose of a header file is not usually to contain
    the code that implements functions, but to "publish" the functions'
    "public faces." The header tells a prospective caller what he needs
    in order to use a function: Its name, its arguments, and its return
    value. The implementation would be in yet another file, the idea
    being that you could apply a fix or improvement to the function code
    without recompiling every module that calls it: As long as the name,
    arguments, and return value remain unchanged, the callers need not
    know about the details of how the function goes about its business.

    So: Technically speaking, this satisfies the "multiple files"
    requirement -- but don't expect to get full marks.

    > #include<stdio.h>
    >
    > int main ()
    > {
    > char part1string[100] = {0}, stop;
    > int part2array[100] = {0}, i = 0;
    >
    >
    > //Part 1
    >
    >
    > printf("Enter the string you would lik to reverse: ");
    > gets(part1string);


    Never use gets(). Not ever. It is an accident that isn't even
    waiting to happen: It's already happened, twenty-four years ago.
    (Look up "Morris Worm.") If you found gets() in a book, take a red
    pencil and draw a big red X through that section. If your instructor
    told you to use gets(), take an axe and draw a big red X through the
    instructor.

    > reverseString(part1string);
    >
    > printf("Your string in reverse: %s", part1string);


    A smallish matter: Make sure there's a newline '\n' character
    at the end of each line you output, including the final one. If
    you don't, you might never see the final line.

    >
    > //Part 2
    >
    >
    > printf("Integer Set Reverser\n");
    >
    > while( stop /= 'y')


    Two problems here: First, you almost certainly mean '!='
    rather than '/='. Both are perfectly legal C operators, and both
    (by ill chance) happen to be syntactically legal in this context,
    but their meanings are wildly different.

    Second, `stop' has not been given any value at all the first
    time your program makes this test. That means you can't rely on
    it having any particular value; specifically, you can't rely on it
    having a value that isn't equal to 'y' ...

    > {
    > printf("Enter an the next integer: ");
    > scanf(" %d", part2array);
    > i++;
    > printf("Do you wish to stop? (y/n): ");
    > scanf(" %c",&stop);
    > }
    >
    >
    > reverseInteger(part2array, i);
    >
    > printf("Your integers in reverse: %s", part2array);


    No, for two reasons. First, `part2array' is not a string, and
    thus cannot be printed with "%s". Second, only the first `i' elements
    of `part2array' are of interest; the others are extraneous zeroes
    that you don't want to print.

    >
    >
    > return 0;
    > }
    >
    > reverseop.c
    >
    >
    > #include<stdio.h>


    You're not doing any input/output in this file, so <stdio.h>
    is unnecessary.

    On the other hand, you *are* using string functions in this
    file, so <string.h> *is* necessary.

    > void reverseString(char *strng);
    > void reverseInteger(int *arrayset[], int size);


    The type of the `arrayset' parameter doesn't match the argument
    the function is called with. (See above, and also see below.)

    >
    > void reverseString(char *strng)
    > {
    > int q = 0, i;
    > char strng2[strlen(strng)];
    >
    >
    > for (i = (strlen(strng)-1); i>= 0; i--)
    > {
    > strng2[q] = strng;
    > q++;
    > }
    >
    > for (i = 0; i< (strlen(strng)-1); i++)
    > {
    > strng = strng2;
    > }


    This should work, I think, but it's working harder than it
    needs to. Imagine that Alice, Barbara, and Clarisse are sitting
    side by side in three chairs, and they decide to reverse their
    order. Your solution is equivalent to setting out three more
    chairs, moving all three girls to the new chairs (in a different
    order), and moving them all back again: Three extra chairs, and
    six total moves. Even if we suppose that only one girl can be on
    her feet at any moment, can you think of a way to do the task with
    just one extra chair and three moves? Can you generalize your new
    method so that Alice,Barbara,Clarisse,...,Xena,Yolanda,Zerbinetta
    can change places using one extra chair (and thirty-nine moves)
    instead of twenty-six extra chairs (and fifty-two moves)?

    > }
    >
    > void reverseInteger(arrayset[], size)


    What are the types of these parameters?

    > {
    > int q = 0, i;
    > char arrayset2[size];


    Why `char'?

    > for (i = (size-1); i>= 0; i--)
    > {
    > arrayset2[q] = arrayset;
    > q++;
    > }
    >
    >
    > for (i = 0; i< (size-1); i++)
    > {
    > arrayset = arrayset2;
    > }
    > }


    Again, you're working too hard: The girls can reverse their
    seating order without so much extra furniture.

    > I get the error expected ) before ] token on this line and I don't
    > understand why
    >
    > void reverseInteger(arrayset[], size)


    See above.

    --
    Eric Sosman
    d
     
    Eric Sosman, Apr 9, 2012
    #4
  5. Tyler

    Eric Sosman Guest

    On 4/8/2012 11:41 PM, Stefan Ram wrote:
    > -berlin.de (Stefan Ram) writes:
    >> Tyler<> writes:
    >>> read a string and print it in reverse (25)

    > [...]
    > That is, of course, unless one is using something like:
    >
    > #include<stdio.h>
    >
    > void reverse( void )
    > { int const c = getchar(); if( c != EOF ){ reverse(); putchar( c ); }}


    Forbidden by the instructions for the exercise:

    >>> Do Not print in reverse functions


    > int main( void ){ reverse(); putchar( '\n' ); }


    --
    Eric Sosman
    d
     
    Eric Sosman, Apr 9, 2012
    #5
  6. Tyler

    Stefan Ram Guest

    Eric Sosman <> writes:
    >>>>Do Not print in reverse functions


    Just for fun I wrote the next program. It does not print in the
    reverse function but admitedly does also not fulfill all requirements
    since it might allocate an array with more than 100 components.
    But at least it does not contain any hardcoded size limitations.

    #include <stdio.h> /* puts, putchar */
    #include <stdlib.h> /* malloc, free */

    char * buff = 0;
    size_t n;

    void reverse( size_t const i )
    { int const c = getchar();
    size_t const j = i + 1;
    if( c != EOF )
    { reverse( j );
    if( buff )buff[ n - j ]=( char )c; }
    else
    { buff = malloc( j );
    n = i;
    if( buff )buff[ i ]= 0; }}

    int main( void )
    { reverse( 0 );
    if( buff ){ puts( buff ); free( buff ); buff = 0; putchar( '\n' ); }}
     
    Stefan Ram, Apr 9, 2012
    #6
  7. On Sun, 8 Apr 2012 19:38:58 -0700 (PDT), Tyler
    <> wrote:

    >For starters here is the homework question:
    >
    >read a string and print it in reverse (25)
    >reverseString function doesn't return anything
    >use one parameter
    >read a set of integers save them into an array and print the set in
    >reverse (25)
    >reverseInteger
    >use two parameters (array and size)
    >maximum array size is 100
    >use a loop to read values, stop when a user enters 'y'
    >Do Not print in reverse functions
    >Bonus
    >If you use pointers to swap values in reverse functions (10)
    >if you implemented with multiple files and Makefile (10)
    >
    >essentially, I think my teacher wants us to modify the array/strings
    >globally but I'm not really sure how to do that and I'm getting some
    >funky errors with my code. However I think I'm really close.


    Why would you think that when your code doesn't even compile?

    >
    >Here's my code:
    >
    >main.c
    >
    >/*
    > * main.c
    > * hw_L9
    > *
    > * Created by Charles Cothren on 4/8/12.


    Since your name is Tyler, who is Charles and why are you posting his
    homework?

    > * Copyright 2012 Syracuse University. All rights reserved.
    > *
    > */
    >
    >#include "reverseop.c"
    >#include <stdio.h>
    >
    >int main ()
    >{
    >char part1string[100] = {0}, stop;
    >int part2array[100] = {0}, i = 0;
    >
    >
    >//Part 1
    >
    >
    >printf("Enter the string you would lik to reverse: ");
    >gets(part1string);


    How will you insure the user does not enter more than 100 characters?

    >
    >reverseString(part1string);


    Where is the prototype for this function?

    >
    >printf("Your string in reverse: %s", part1string);


    It is normally advisable to end a printf with a \n.

    >
    >
    >//Part 2
    >
    >
    >printf("Integer Set Reverser\n");
    >
    >while( stop /= 'y')


    Why would you want to divide the value in stop by the value 'y'? Where
    is stop defined?

    >{
    >printf("Enter an the next integer: ");
    >scanf(" %d", part2array);


    Where is i defined?

    >i++;
    >printf("Do you wish to stop? (y/n): ");
    >scanf(" %c", &stop);
    >}
    >
    >
    >reverseInteger(part2array, i);
    >
    >printf("Your integers in reverse: %s", part2array);


    The %s promises printf that the corresponding argument is a char*.
    Does the expression part2array evaluate to anything with type pointer
    to char?

    >
    >
    >


    Do you get paid by the line? While some vertical white space makes
    the code easier to understand, excessive blank lines just force people
    to scroll back and forth endlessly.

    >return 0;
    >}
    >
    >reverseop.c
    >
    >
    >#include <stdio.h>
    >
    >void reverseString(char *strng);
    >void reverseInteger(int *arrayset[], int size);


    This says that the first parameter is an array of pointers.

    >
    >void reverseString(char *strng)


    Let's play computer and process "abc".

    >{
    >int q = 0, i;
    >char strng2[strlen(strng)];


    Where is the prototype for strlen? Of course this isn't legal since
    the dimension of an array definition must be a compile time constant.

    >
    >
    >for (i = (strlen(strng)-1); i >= 0; i--)
    >{
    >strng2[q] = strng;
    >q++;
    >}


    So strng2 contains 'c', 'b', 'a'. It is not a string since it does
    not contain a terminating '\0'.

    >
    >for (i = 0; i < (strlen(strng)-1); i++)


    strlen(strng) evaluates to 3. strlen(strng)-1 evaluates to 2. You
    iterate this loop for i with values 0 and 1. You never iterate for i
    set to 2.

    >{
    >strng = strng2;


    As a result, strng now contains "cbc" since you never copied the 'a'
    over from strng2[2].

    >}
    >
    >}
    >
    >void reverseInteger(arrayset[], size)


    Don't you think the parameters should have a type? This line will
    generate so many compiler diagnostics the compiler will just give up.

    >{
    >int q = 0, i;
    >char arrayset2[size];


    Why is this an array of char when you are supposed to be processing an
    array of int.

    >
    >for (i = (size-1); i >= 0; i--)
    >{
    >arrayset2[q] = arrayset;
    >q++;
    >}


    You will save yourself a whole lot of aggravation if you learn to
    indent consistently.

    >
    >
    >for (i = 0; i < (size-1); i++)
    >{
    >arrayset = arrayset2;


    Again, you don't process the last element.

    >}
    >}
    >
    >I get the error expected ) before ] token on this line and I don't
    >understand why
    >
    >void reverseInteger(arrayset[], size)


    Because it is gibberish. Look at the definition of reverseString for
    a hint about something omitted.

    In addition to the error you describe, my compiler produced nine other
    diagnostics, some of which are fatal to your effort. If your compiler
    doesn't, then you need to up the warning level and turn off
    non-standard extensions.

    --
    Remove del for email
     
    Barry Schwarz, Apr 9, 2012
    #7
  8. Tyler

    James Kuyper Guest

    On 04/09/2012 02:56 PM, Barry Schwarz wrote:
    > On Sun, 8 Apr 2012 19:38:58 -0700 (PDT), Tyler
    > <> wrote:

    ....
    >>
    >>
    >>

    >
    > Do you get paid by the line? While some vertical white space makes
    > the code easier to understand, excessive blank lines just force people
    > to scroll back and forth endlessly.


    I think you mean "needlessly". If it is indeed "endlessly", you've just
    discovered a new alternative to the life imprisonment. :)

    ....
    >> {
    >> int q = 0, i;
    >> char strng2[strlen(strng)];

    >
    > Where is the prototype for strlen? Of course this isn't legal since
    > the dimension of an array definition must be a compile time constant.


    Not in C99. An automatic array with a dimension that is not a constant
    expression defines a VLA. Do you have any reason to believe that he's
    restricted to using C90?
     
    James Kuyper, Apr 9, 2012
    #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. dogbite
    Replies:
    4
    Views:
    701
    osmium
    Oct 10, 2003
  2. umdehoon

    In-place array modification

    umdehoon, Jul 18, 2004, in forum: Python
    Replies:
    3
    Views:
    423
    Jeff Epler
    Jul 19, 2004
  3. j_hop_97

    unexpected array modification

    j_hop_97, Sep 9, 2006, in forum: C Programming
    Replies:
    4
    Views:
    349
    Coos Haak
    Sep 9, 2006
  4. Rubist Rohit
    Replies:
    19
    Views:
    628
    Adam Prescott
    Apr 23, 2011
  5. Sia
    Replies:
    24
    Views:
    273
    Nick Mellor
    Jan 7, 2013
Loading...

Share This Page